OAuth:在模拟器上与yahoo连接时,retrieveAccessToken()出现异常?

时间:2013-04-19 09:31:46

标签: android android-emulator android-networking android-embedded-api

集成到雅虎时出现上述错误

提供商定义如下

provider = new CommonsHttpOAuthProvider(
                "https://api.login.yahoo.com/oauth/v2/get_request_token",
                "https://api.login.yahoo.com/oauth/v2/get_token",
                "https://api.login.yahoo.com/oauth/v2/request_auth");

消费者和Oauth_Verifier

            consumer = new CommonsHttpOAuthConsumer(
                "ppp",
                "lll");

        consumer.setMessageSigner(new HmacSha1MessageSigner());

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

尝试从yahoo api访问令牌时显示错误: -

oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null

当在代码下面运行时,异常进入上面: -

 provider.retrieveAccessToken(consumer, oauth_verifier);

1 个答案:

答案 0 :(得分:1)

BIG EDIT

高度建议不要使用Signpost,除非您有关于如何处理令牌刷新的计划。路标没有实现这一目标的方法。我切换到Scribe,这更容易使用。不幸的是它也没有帮助Token Refresh的方法,但我能够很容易地编写一个帮助方法。

我现在让Yahoo API在Android上100%运行。如果您无法通过Signpost弄明白并将切换到Scribe,我会发布我的代码。


从头到尾这对我有用。我也可以重用一把钥匙。如果你做了一件不合适的事情就会让人感到烦恼。这是我打算清理的粗略版本。仍然需要在60分钟后找出更新令牌。

Android清单

<activity
    android:name=".UpdaterActivity"
    android:label="@string/app_name"
    android:launchMode="singleInstance" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter> 
        <action android:name="android.intent.action.VIEW" /> 
        <category android:name="android.intent.category.DEFAULT" /> 
        <category android:name="android.intent.category.BROWSABLE" /> 
        <data android:scheme="yfbwa" android:host="oauth"></data> 
    </intent-filter> 
</activity>

<强> UpdaterActivity.java

private static final String CONSUMER_KEY = "";
private static final String CONSUMER_SECRET = "";
private static final String REQUEST_TOKEN_URL = "https://api.login.yahoo.com/oauth/v2/get_request_token";
private static final String ACCESS_TOKEN_URL = "https://api.login.yahoo.com/oauth/v2/get_token";
private static final String AUTHORIZE_WEBSITE_URL = "https://api.login.yahoo.com/oauth/v2/request_auth";
private static final String CALLBACK_URL = "yfbwa://oauth";
private OAuthConsumer consumer;
private OAuthProvider provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
    provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_WEBSITE_URL);
    if(getIsAuthorized() == 0) {
        callOAuth();
    } else {
        makeRequest("http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=308/teams?format=json");
    }
}

private void callOAuth() { 
    String url = provider.retrieveRequestToken(consumer, CALLBACK_URL);
    this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
}

@Override 
protected void onNewIntent(Intent intent) { 
    Uri data = intent.getData();
    String verifier = data.getQueryParameter(OAuth.OAUTH_VERIFIER);
    provider.retrieveAccessToken(consumer, URLDecoder.decode(verifier,"UTF-8"));
    writePrefs(consumer.getToken(), consumer.getTokenSecret());
    makeRequest("http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=308/teams?format=json");
}

private void makeRequest(String url) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException, ClientProtocolException, IOException {
    String[] prefs = getPrefs();
    consumer.setTokenWithSecret(prefs[0], prefs[1]);
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    consumer.sign(request);
    HttpResponse response = httpClient.execute(request);
    String result = EntityUtils.toString(response.getEntity());
}

private void writePrefs(String token, String tokenSecret) {
    Editor prefsEditor = getSharedPreferences("ybfwa", MODE_PRIVATE).edit();
    prefsEditor.putString("token",token);
    prefsEditor.putString("tokenSecret",tokenSecret);
    prefsEditor.putInt("isAuthorized",1);
    prefsEditor.commit();
}

private String[] getPrefs() {
    String[] prefs = new String[2];
    SharedPreferences myPrefs = getSharedPreferences("ybfwa", MODE_PRIVATE);
    prefs[0] = myPrefs.getString("token", null);
    prefs[1] = myPrefs.getString("tokenSecret", null);
    return prefs;
}

private int getIsAuthorized() {
    SharedPreferences myPrefs = getSharedPreferences("ybfwa", MODE_PRIVATE);
    return myPrefs.getInt("isAuthorized", 0);
}