开发者

Can't authorize my app using twitter4j

开发者 https://www.devze.com 2023-04-04 19:13 出处:网络
I\'m writing an Android twitter app using twitter4j 2.2.4, and I\'m having trouble getting past OAuth.

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.upd开发者_如何学CateStatus(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?


I realize this is an old question, but I used your TwitterBackend class and I had the same issue. I resolved this by making one minor change (can't say why it works, yours should have been fine).

Change this

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

To this

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

I'm unable to explain why, but I just started going down each method listed at twitter4j for getOAuthAccessToken() and got one that works.

I hope this helps someone, but thank you for the nice base class to work with.


I don't think this is a problem with your code. This is a problem with the whole Twitter authorisation mechanism, it seems that if your are logged in from the Twitter App on your phone, into Twitter then the OAuth authorization gives you this error. I guess it takes the credentials used by the Twitter App and tries to acces the App using them, and that's why you get the "incorrect credentials" message.

You can try to logout from the Twitter App of your phone, and then try again with OAuth. It worked for me, but I still couldn't find how to solve the problem when you are logged into the Twitter App and you also try to log in from your app using OAuth.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号