Android OAuth: Exception on retrieveAccessToken()

拥有回忆 提交于 2019-11-30 02:26:48

Okay, I figured it out. Maybe this is helpful to others:

First of all, you do not need to save the whole consumer and provider object. All you need to do is store the requestToken and the requestSecret. Luckily, those are Strings, so you don't need to write them to disk or anything. Just store them in the sharedPreferences or something like that.

Now, when you get redirected by the browser and your onResume() method is called, just do the following:

//The consumer object was lost because the browser got into foreground, need to instantiate it again with your apps token and secret.
consumer = new CommonsHttpOAuthConsumer("xxx", "yyy"); 

//Set the requestToken and the tokenSecret that you got earlier by calling retrieveRequestToken.
consumer.setTokenWithSecret(requestToken, tokenSecret);

//The provider object is lost, too, so instantiate it again.
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token", 
                                "https://api.twitter.com/oauth/access_token", 
                                "https://api.twitter.com/oauth/authorize");     
//Now that's really important. Because you don't perform the retrieveRequestToken method at this moment, the OAuth method is not detected automatically (there is no communication with Twitter). So, the default is 1.0 which is wrong because the initial request was performed with 1.0a.
provider.setOAuth10a(true);

provider.retrieveAccessToken(consumer, verifier);

That's it, you can receive the token and the secret with getToken() and getTokenSecret(), now.

Hi Manuel i see you are avoidin the OAuthocalypse too! heres is a good example to implement OAuth for Twitter using sharedPreferences to save requestToken and the requestSecret, like your solution. http://github.com/brione/Brion-Learns-OAuth by Brion Emde

heres the video

hope this helps other developers =)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!