当集成到yahoo
时出现上述错误provider define
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_验证者
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);
我强烈建议不要使用路标,除非你有一个如何处理令牌刷新的计划。Signpost没有实现这一点的方法。我改用Scribe,它更容易使用。不幸的是,它也没有帮助Token刷新的方法,但我能够相当容易地编写一个助手方法。
我现在有雅虎API在Android上100%工作。如果你不能用路标找出它,并将切换到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);
}