好的,所以,我正在为Android制作Tumblr客户端,我一直在尝试并且未能让OAuth工作一周左右.这是它的发展方向:
用户启动应用程序.主要活动的onCreate执行此操作:
settings = getSharedPreferences(PREFS_NAME, 0);
authToken=settings.getString("OauthToken", "none");
authTokenSecret=settings.getString("OauthSecret", "none");
if(authToken=="none" || authTokenSecret=="none"){
Intent i = new Intent(getApplicationContext(),Authentication.class);
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
这将启动包含WebView的身份验证活动.该活动成功获取请求令牌,并将WebView发送到Tumblr登录屏幕.要求用户允许应用访问他们的数据,他们按下允许,我的WebViewClient捕获回调URL,并使用它执行此操作:
String[] token = helper.getVerifier(url);
if (token != null) {
try {
String accessToken[] = helper.getAccessToken(token[1]);
editor.putString("OauthToken", accessToken[0]);
editor.putString("OauthSecret", accessToken[1]);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
finish();
Run Code Online (Sandbox Code Playgroud)
辅助类的getAccessToken和getVerifier看起来像这样:
public String[] getVerifier(String myUrl) {
// extract the token if it exists
Uri uri = Uri.parse(myUrl);
if (uri == null) {
return null;
}
String token = uri.getQueryParameter("oauth_token");
String …Run Code Online (Sandbox Code Playgroud)