安卓OAuth:在retrieveAccessToken()上出现异常

8
我正在为我的Android应用设置OAuth。为了测试它,我做了以下几步: 1.将signpost-core-1.2.1.1.jar和signpost-commonshttp4-1.2.1.1.jar添加到我的项目中。 2.添加变量"CommonsHttpOAuthConsumer consumer"和"CommonsHttpOAuthProvider provider"。 3.当按钮被点击时,执行以下操作:
consumer = new CommonsHttpOAuthConsumer("xxx", "yyy");
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token", 
                    "https://api.twitter.com/oauth/access_token", 
                    "https://api.twitter.com/oauth/authorize");

oauthUrl = provider.retrieveRequestToken(consumer, "myapp://twitterOauth");
persistOAuthData();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(oauthUrl)));

persistOAuthData()的功能如下:

protected void persistOAuthData()
{
    try
    {
        FileOutputStream providerFOS = this.openFileOutput("provider.dat", MODE_PRIVATE);
        ObjectOutputStream providerOOS = new ObjectOutputStream(providerFOS);
        providerOOS.writeObject(this.provider);
        providerOOS.close();

        FileOutputStream consumerFOS = this.openFileOutput("consumer.dat", MODE_PRIVATE);
        ObjectOutputStream consumerOOS = new ObjectOutputStream(consumerFOS);
        consumerOOS.writeObject(this.consumer);
        consumerOOS.close();
    }
    catch (Exception e) { }
}

因此,在打开浏览器之前,消费者和提供者都已经保存好了,就像这里所描述的一样。

在onResume()方法中,我加载提供者和消费者数据,并执行以下操作:

    Uri uri = this.getIntent().getData();
    if (uri != null && uri.getScheme().equals("myapp") && uri.getHost().equals("twitterOauth"))
    {
        verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
        if (!verifier.equals(""))
        {
            loadOauthData();
            try
            {
                provider.retrieveAccessToken(consumer, verifier);
            }
            catch (OAuthMessageSignerException e) {
                e.printStackTrace();
            } catch (OAuthNotAuthorizedException e) {
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                e.printStackTrace();
            }            
        }
    }

所以,以下是有效的: 1)我得到了一个requestToken和一个requestSecret。 2)我得到了oauthUrl。 3)我被导向浏览器页面授权我的应用程序 4)我被重定向到我的应用程序。 5)我得到了验证器。 但是调用retrieveAccessToken(consumer,verifier)失败,并显示OAuthCommunicationException,指出“与服务提供商的通信失败:null”。
有人知道可能的原因吗?有些人似乎在获取requestToken时遇到问题,但这很好。我想知道是否我的应用程序也包括我需要进行多部分上传的apache-mime4j-0.6.jar和httpmime-4.0.1.jar,这可能是一个问题。
2个回答

13

好的,我已经理解了。也许这对其他人有帮助:

首先,您不需要保存整个消费者和提供者对象。您只需要存储requestToken和requestSecret即可。幸运的是,它们都是字符串,所以您不需要将它们写入磁盘或其他什么地方。只需将它们存储在sharedPreferences或类似的位置即可。

现在,当您被浏览器重定向并调用onResume()方法时,请执行以下操作:

//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);

就是这样,您可以使用getToken()和getTokenSecret()函数获取令牌和密钥。


那么在您的情况下,您在consumer.setOkenWithSecret中设置的requestTokenoauthUrl吗?我能问一下tokenSecret是什么吗?谢谢 :) - dumbfingers
抱歉,我想我可能已经弄清楚了,requestToken 是通过使用 consumer.getToken() 检索的吗? - dumbfingers

0

嗨,Manuel,我看到你也在避免OAuthocalypse!这里有一个很好的例子,使用sharedPreferences保存requestToken和requestSecret来实现Twitter的OAuth,就像你的解决方案一样。 http://github.com/brione/Brion-Learns-OAuth 作者:Brion Emde

这是视频

希望这能帮助其他开发者 =)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接