Home » Android » twitter not working with twitter4j in android

twitter not working with twitter4j in android

Posted by: admin June 16, 2020 Leave a comment


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

I looked at similar questions to this 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.");

            Log.e(TAG, "Interent connection not available");

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

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

        Thread thread = new Thread(new Runnable() {
            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

                    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

                } catch (TwitterException te) {

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

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



    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 where i am calling loginToTwitter function.

protected void onCreate(Bundle savedInstanceState) {


    OBLTwitter twitter = new OBLTwitter(this);

This is my manifest file, I am adding this because i made some changes suggested by answerers of different questions.

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

    android:theme="@style/AppTheme" >
        android:label="@string/app_name" >

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

                android:scheme="app" />

Error log

enter image description here

How to&Answers:

The reason you are getting an HTTP 403 (Forbbiden) status code most likely has to do with the fact that Twitter has recently restricted its traffic to TLS only.

From Twitter’s calendar of API changes:

January 14, 2014 Restricting api.twitter.com to TLS traffic only
API v1.1

Several libraries that were using plain HTTP by default have been updated in order to comply with this. So was the case with Twitter4j. On 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 might still be able to use your current version by doing a minor change to your code. The ConfigurationBuilder class has a setSSL method, which you can set to true to use HTTPS. So you might get it to work by adding the following line to your code:



The Status 403 error indicates that there’s a problem with your login.

  • Check that you have successfully created an app on dev.twitter.com
  • Make sure you’re using the right keys in the right places
  • Is your username / password correct
  • Finally, are you using the latest version of Twitter4J? Make sure that it is only using HTTP*S* URLs.


Noted is that Twitter has changed their url “http://api.twitter.com/1.1
into ‘https://api.twitter.com/1.1

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

So, maybe your lib just out of date. You can try to use others library. (I’m using https://github.com/3pillarlabs/socialauth and it quite good)


Just update your twitter jar with the latest version.


Try this:

mTwitter = new TwitterFactory().getInstance();

mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey);
mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token",

    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 your project: