Search code examples
androidtwittertwitter-oauthtwitter4j

Can't authorize my app using twitter4j


I'm writing an Android twitter app using twitter4j 2.2.4, and I'm having trouble getting past OAuth.

here's my code :

package info.mahmoudhossam.twitter;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class TwitterActivity extends Activity {

    private static int OAUTH_REQUEST = 1;
    private static String consumerKey = "##########";
    private static String consumerSecret = "################";
    private Twitter twitter;
    private AccessToken accessToken;
    private TwitterBackend backend;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button loginButton = (Button) findViewById(R.id.button1);
        registerForContextMenu(loginButton);
        backend = new TwitterBackend();
        twitter = backend.getTwitterInstance(consumerKey, consumerSecret);
    }

    public void onLogin(View view) {
        Intent intent = new Intent("mahmoud.browser");
        try {
            Log.i("URL", backend.getRequestToken().getAuthenticationURL());
            intent.putExtra("url", backend.getRequestToken().getAuthenticationURL());
        } catch (TwitterException e) {
            Log.e("Twitter", e.getMessage());
            return;
        }
        startActivityForResult(intent, OAUTH_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == OAUTH_REQUEST && resultCode == RESULT_OK) {
            Uri url = Uri.parse(data.getExtras().getString("url"));
            String verifier = url.getQueryParameter("oauth_verifier");
            Log.i("Verifier", verifier);
            try {
                accessToken = backend.getAccessToken(verifier);
            } catch (TwitterException e) {
                Log.e("Twitter", "Exception occurred, quitting");
                return;
            }
            twitter.setOAuthAccessToken(accessToken);
            try {
                if(twitter.verifyCredentials() != null){
                    Toast.makeText(getApplicationContext(), "Logged in!", Toast.LENGTH_SHORT);
                }
            } catch (TwitterException e) {
                Log.e("Twitter", e.getMessage());
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this,
                    "Cannot connect to twitter, app not authorized",
                    Toast.LENGTH_SHORT).show();
        }
    }

}

And the TwitterBackend class :

package info.mahmoudhossam.twitter;

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class TwitterBackend {
    private static Twitter twitter;

    public Twitter getTwitterInstance(String consumerKey,
            String consumerSecret){
        if(twitter == null){
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(consumerKey, consumerSecret);
        }
        return twitter;
    }

    public RequestToken getRequestToken()
            throws TwitterException {
        return twitter.getOAuthRequestToken();
    }

    public AccessToken getAccessToken(String verifier)
            throws TwitterException{
        return twitter.getOAuthAccessToken(getRequestToken(), verifier);
    }

    public List<Status> getTimeline() throws TwitterException{
        return twitter.getHomeTimeline();
    }

    public void updateStatus(String status) throws TwitterException{
        twitter.updateStatus(status);
    }


}

I'm getting a 401 error from twitter saying that authentication credentials are missing or incorrect, and I'm sure I've entered the correct consumer key/secret.

Is there something wrong with the code itself?


Solution

  • In case someone gets stuck with this, I found a solution to my problem.

    It was the emulator's clock being out of sync, starting emulators from snapshots used to mess up the clock.

    So if you have this problem, check the emulator's time and date, and if it's out of sync, correct it.