【问题标题】:twitter not working with twitter4j in androidtwitter 在 android 中不能与 twitter4j 一起使用
【发布时间】:2014-03-14 19:31:47
【问题描述】:

我正在使用 twitter4j 将 twitter 集成到 android 中。 这是我的推特代码。我正在为 twitter 创建单独的类。

我查看了与此类似的问题,但对我没有任何帮助。 Question 和其他一些问题。

Twitter 类

public class OBLTwitter {

private static final String TAG = "OBLTwitter";
private Activity activity;

// Twitter constants
private static final String TWITTER_CONSUMER_KEY = "KEY";
private static final String TWITTER_CONSUMER_SECRET = "SECRET";
public static final String TWITTER_CALLBACK_URL = "app://ridesharebuddy";

// Twitter variables
private static Twitter twitter;
private static RequestToken requestToken;
public static boolean userDeniedToContinue;

    public OBLTwitter(Activity activity) {

        Log.d(TAG, "Parameterized constructor called.");

        this.activity = activity;
        userDeniedToContinue = false;
    }


    // Login to twitter
    public void loginToTwitter() {

        Log.e(TAG, "Logging in to twitter.");

        if(!isNetworkAvailable(this.activity))
        {
            Log.e(TAG, "Interent connection not available");
        }

        // Set up Twitter object
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
        builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
        Configuration configuration = builder.build();

        TwitterFactory factory = new TwitterFactory(configuration);
        twitter = factory.getInstance();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    if(twitter == null)
                    {
                        Log.e(TAG, "twitter is null");
                    }
                    Log.e("called", "called run method");
                    // Get RequestToken and call authentication URL to show
                    // twitter page
                    requestToken = twitter
                            .getOAuthRequestToken(TWITTER_CALLBACK_URL);

                    Log.e(TAG, "getting request token");

                    Log.e("oAuth token :", requestToken.getToken());
                    Log.e("oAUth secret:", requestToken.getTokenSecret());
                    Log.e("oAuth URL: ", requestToken.getAuthenticationURL());

                     activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                     .parse(requestToken.getAuthenticationURL())));

                } catch (TwitterException te) {

                    Log.e(TAG, "Twitter exception, Login error.");
                    te.printStackTrace();                       

                    Log.e(TAG, "Error code : " + te.getErrorCode());
                    Log.e(TAG, "Error message : " + te.getErrorMessage());
                    Log.e(TAG, "Status code : " + te.getStatusCode());
                    Log.e(TAG, "Access level : " + te.getAccessLevel());

                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        });

        thread.start();
    }

    public boolean isNetworkAvailable(Context context)
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

}

这是我调用 loginToTwitter 函数的活动代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    OBLTwitter twitter = new OBLTwitter(this);
    twitter.loginToTwitter();
}

这是我的 ma​​nifest 文件,我添加这个是因为我做了一些不同问题的回答者建议的更改。

 <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.objectlounge.OBLTwitter.MainActivity"
        android:label="@string/app_name" >
        android:launchMode="singleInstance">


        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="ridesharebuddy"
                android:scheme="app" />
        </intent-filter>
    </activity>
</application>

错误日志

【问题讨论】:

标签: android twitter twitter4j


【解决方案1】:

您获得HTTP 403 (Forbbiden) 状态代码的原因很可能与 Twitter 最近限制其流量到 的事实有关仅 TLS

来自Twitter's calendar of API changes

2014 年 1 月 14 日将 api.twitter.com 限制为仅限 TLS 流量 API v1.1

为了遵守这一规定,默认使用纯 HTTP 的几个库已更新。 Twitter4j 的情况也是如此。在 3.0.5 版本上,它现在默认使用 TLS。

现在,如果出于某种原因您真的不想更新库,您可能仍然可以通过对代码进行微小更改来使用当前版本。 ConfigurationBuilder 类有一个 setSSL 方法,您可以将其设置为 true 以使用 HTTPS。因此,您可以通过在代码中添加以下行来使其工作:

builder.setSSL(true);

【讨论】:

    【解决方案2】:

    Status 403 错误表示您的登录存在问题。

    • 检查您是否已在 dev.twitter.com 上成功创建应用
    • 确保在正确的位置使用正确的键
    • 您的用户名/密码是否正确
    • 最后,你用的是最新版的 Twitter4J 吗?确保它仅使用 HTTP*S* URL。

    【讨论】:

      【解决方案3】:

      值得注意的是,Twitter 已更改其网址“http://api.twitter.com/1.1” 进入'https://api.twitter.com/1.1''

      更多细节在这里:https://groups.google.com/forum/#!topic/socialauth-users/AE0Xy6kRRHc

      所以,也许你的库刚刚过时。您可以尝试使用其他库。 (我用的是https://github.com/3pillarlabs/socialauth,挺好用的)

      【讨论】:

        【解决方案4】:

        只需使用最新版本更新您的 twitter jar。

        【讨论】:

          【解决方案5】:

          试试这个:

          mTwitter = new TwitterFactory().getInstance();
          
          mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey);
          mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token",
                  "https://twitter.com/oauth/access_token",
                  "https://twitter.com/oauth/authorize");
          
          try{
              authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
          }catch(OAuthCommunicationException oACEx){
              Log.d("", "");
          }catch(OAuthMessageSignerException oAMSEx){
              Log.d("", "");
          }catch(OAuthNotAuthorizedException oANAEx){
              Log.d("", "");
          }catch(OAuthExpectationFailedException oAEFEx){
              Log.d("", "");
          }
          

          但是你需要在你的项目中添加一些jar库:

          https://code.google.com/p/oauth-signpost/downloads/detail?name=signpost-core-1.2.1.1.jar&can=2&q=

          https://code.google.com/p/oauth-signpost/downloads/detail?name=signpost-commonshttp4-1.2.1.1.jar&can=2&q=

          https://code.google.com/p/oauth-signpost/downloads/detail?name=signpost-jetty6-1.2.1.1.jar&can=2&q=

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-27
            • 2013-01-17
            • 1970-01-01
            • 1970-01-01
            • 2017-05-18
            • 2011-10-21
            • 2018-05-08
            • 2013-07-04
            相关资源
            最近更新 更多