Twitter does not work with twitter4j in android

I am using twitter4j to integrate twitter into android. This is my twitter code. I am creating a separate twitter class.

I looked at similar questions, but nothing works for me. Question and some other questions.

Twitter class

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

}

This is my activity code from which I call the loginToTwitter function.

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); OBLTwitter twitter = new OBLTwitter(this); twitter.loginToTwitter(); } 

This is my manifest file, I am adding this because I made some changes suggested by the respondents of various questions.

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

Error log

enter image description here

+8
android twitter twitter4j
source share
5 answers

The reason you get an HTTP 403 status code (Forbbiden) is most likely due to the fact that Twitter has recently limited its traffic to only TLS .

From Twitter API API changes :

January 14, 2014 Traffic restriction api.twitter.com only on TLS API v1.1

Several libraries that used plain HTTP by default have been updated to match this. So it was with Twitter4j . In version 3.0.5, it now uses TLS by default.

Now, if for some reason you really don't want to update the library, you can still use your current version by making minor changes to your code. The ConfigurationBuilder class has a setSSL method that you can set to true to use HTTPS. Therefore, you can make it work by adding the following line to your code:

 builder.setSSL(true); 
+6
source share

A Status 403 error indicates a problem with your login.

  • Make sure you have successfully created the app at dev.twitter.com
  • Make sure you use the right keys in the right places.
  • Is your username / password correct?
  • Finally, are you using the latest version of Twitter4J? Make sure it uses only the HTTP * S * URL.
+1
source share

It is noted that Twitter has changed its url " http://api.twitter.com/1.1 " to ' https://api.twitter.com/1.1 ' '

More details here: https://groups.google.com/forum/#!topic/socialauth-users/AE0Xy6kRRHc

So maybe your lib is just out of date. You can try using a different library. (I am using https://github.com/3pillarlabs/socialauth and this is not bad)

+1
source share

Just update your twitter jar with the latest version.

0
source share

Try the following:

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

But you need to add some jar libraries to the project:

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=

0
source share

All Articles