2014-02-15 52 views
8

我正在使用twitter4j在twitter中集成twitter。 这是我的代码来推特。我正在为twitter创建单独的类。微博不能在twitter中使用twitter4j

我看了类似的问题,但没有为我工作。 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(); 
} 

这是我清单文件,我加入了这一点,因为我做的不同的问题应答者提出了一些变化。

<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> 

错误日志

enter image description here

+0

我有一些很好的示例代码在这里发布https://github.com/ ericwoodruff/android_twitter_oauth –

+0

非常感谢。我下载它作为参考。我试图运行它,但它给了我onActivityResult错误。它进入取消的开关部分块,然后崩溃。它没有显示我登录并显示取消。 – keen

+0

它可以假设你已经创建了一个twitter4j.properties并将它放在类路径中,并带有你的秘密。 http://twitter4j.org/en/configuration.html –

回答

6

您购买的​​(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); 
1

Status 403错误表明有一个与您的登录问题。

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

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

0

试试这个:

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("", ""); 
} 

但是,你需要一些罐子库添加到您的项目:

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=