开发者

Android Twitter can't retrieve accestoken

I get the error " Requrest token or token secret not set in server reply. The service provider you use is probably buggy. " when calling the

provider.RetrieveAccesstoken(comsumer, verifier);

But the weird thing is i only get this error sometimes. first my code worked en i posted a message. Then the next day i tried to reinstall the app on my

home computer i got this error. back at my work station i got the same error again but after pressing the button that initiates the procedure it worked again and

posted a message on Twitter. What the hell is going on?! could somebody explain?

i run 2 emulators on android 2.2 it works more often on the WQVGA400 then on the WVGA800 emulator :S

i also get the notification "Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required."

Does this have to do something with my memory or storing the Token secrets? it sounds like the service is too busy to reply when contacting it.

CODE

For readibility i removed the exceptions and imports

public class TestAgainActivity extends Activity {

private String CONSUMER_KEY = "xxxxx";
private String CONSUMER_SECRET = "xxxxxxxxxxxx";
private String CALLBACK_URL = 开发者_运维技巧"myapp://twit";
private OAuthConsumer consumer; 
private OAuthProvider provider;
private String authUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button loginButton = new Button(this);
    loginButton = (Button)findViewById(R.id.button1);
    loginButton.setOnClickListener(login);

    consumer = new CommonsHttpOAuthConsumer(  
            CONSUMER_KEY, CONSUMER_SECRET);  

    provider = new DefaultOAuthProvider(
            "https://api.twitter.com/oauth/request_token",
            "https://api.twitter.com/oauth/access_token",  
            "https://api.twitter.com/oauth/authorize");  

}
private OnClickListener login = new OnClickListener() {
    public void onClick(View v) {
        AskOauth();
    }           

};

public void AskOauth(){

    try {

        authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);

    Here i store the Consumer token and tokenSecret
        SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
        SharedPreferences.Editor editor = savedTokens.edit();
        editor.putString("savedToken", consumer.getToken());
        editor.putString("savedTokenSecret", consumer.getTokenSecret());
        editor.commit();

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));

    } catches{
}

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
    String saved_token = savedTokens.getString("savedToken", "empty ShP");
    String saved_tokenSecret = savedTokens.getString("savedTokenSecret", "empty ShP");
    consumer.setTokenWithSecret(saved_token, saved_tokenSecret);

    System.out.println("consumer token "+consumer.getToken());
    System.out.println("consumer tokenSecret "+consumer.getTokenSecret());

Uri uri = this.getIntent().getData();  


    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {

      SharedPreferences savedTokens = getSharedPreferences("savedTokens", 0);
      String saved_token = savedTokens.getString("savedToken", "empty ShP");
  String saved_tokenSecret = savedTokens.getString("savedTokenSecret", "empty ShP");
      consumer.setTokenWithSecret(saved_token, saved_tokenSecret);

        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);  

        try {

        Here i sometime get the error! 
        provider.retrieveAccessToken(consumer, verifier);
        AccessToken accesToken = new AccessToken(consumer2.getToken(), consumer2.getTokenSecret());

        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                twitter.setOAuthAccessToken(accesToken);
                Date d = new Date(System.currentTimeMillis());
                String tweet = "This message is tweeted " + d.toLocaleString();
                try {
                twitter.updateStatus(tweet);
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }      

        } catch (OAuthMessageSignerException e1) {
        }


It worked! instead of using the Oath signpost library

provider.retrieveAccessToken(consumer,verifier)

i used the twitter4j library

getOAuthAccessToken(requestToken, verifier)

    String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    AccessToken accessToken = null;//get stored request tokens from database
    mDbHelper = DbAdapter.getInstance();
    mDbHelper.open();
    String[] dbRequestTokens = mDbHelper.getTwitterRequestTokens();
    mDbHelper.close();
    RequestToken requestToken = new RequestToken(dbRequestTokens[0], dbRequestTokens[1]);
    try {
        accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
        //dataBase store accessTokens to tweet message  or acces other twitter recources, and don't forget to set the OAuth Consumer with consumer key and secret
        mDbHelper.open();
        mDbHelper.setTwitterAccessTokens(accessToken.getToken(), accessToken.getTokenSecret());
        mDbHelper.close();
        return "logged in succesfully";
        } catch (TwitterException e) {
        e.printStackTrace();
        return e.getMessage();
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜