大编辑
我强烈建议不要使用 Signpost,除非您有如何处理令牌刷新的计划。路标没有方法来实现这一点。我切换到更容易使用的Scribe 。不幸的是,它也没有帮助令牌刷新的方法,但我能够相当容易地编写一个辅助方法。
我现在让 Yahoo API 在 Android 上 100% 运行。如果您无法使用 Signpost 解决问题并切换到 Scribe,我将发布我的代码。
这对我来说从头到尾都有效。我还可以重复使用密钥。棘手的事情,如果你不按顺序做一件事,它就会中断。这是我打算清理的粗略版本。60 分钟后仍然需要计算更新令牌。
安卓清单
<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);
}