Nic*_*ick 5 android oauth tumblr signpost
好的,所以,我正在为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 verifier = uri.getQueryParameter("oauth_verifier");
return new String[] { token, verifier };
}
public String[] getAccessToken(final String verifier)
throws OAuthMessageSignerException, OAuthNotAuthorizedException,
OAuthExpectationFailedException, OAuthCommunicationException {
new Thread(new Runnable() {
public void run() {
try {
mProvider.retrieveAccessToken(mConsumer, verifier);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
return new String[] {
mConsumer.getToken(), mConsumer.getTokenSecret()
};
}
Run Code Online (Sandbox Code Playgroud)
然后我终于回到主应用程序屏幕并尝试进行第一次API调用,以获取用户仪表板上最近的十个帖子:
OAuthConsumer myConsumer = new CommonsHttpOAuthConsumer(MainView.authToken, MainView.authTokenSecret);
HttpGet request = new HttpGet("http://api.tumblr.com/v2/user/dashboard?limit=10");
myConsumer.sign(request);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有得到像我应该得到的好的JSON响应,而是得到了这个:
10-20 16:36:18.110: D/Result(22817): {"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
Run Code Online (Sandbox Code Playgroud)
那我哪里出错了?谢谢
我已成功将路标库与 Appache HttpCommons GET/POST/PUT 结合使用。
首先,我使用以下代码启动了用于登录的 WebView ( ActivityLogin.java):
private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);
private static OAuthProvider provider = new DefaultOAuthProvider (AppConfig.requestURL, AppConfig.accessURL, AppConfig.authURL);
private void logMeIn() throws ...{
String authUrl = provider.retrieveRequestToken(consumer,AppConfig.CALLBACK_URL);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}
Run Code Online (Sandbox Code Playgroud)
然后,我用于onNewIntent(Intent)接收来自先前启动的 OAuth 回调Activity:
AppConfig.java代码片段:
/** OAuth Authorization URL */
public static final String authURL = mainOAuthUrl+"/authorize/?theme=android";
/** OAuth Request URL */
public static final String requestURL = mainOAuthUrl+"/request/token/";
/** OAuth Access URL */
public static final String accessURL = mainOAuthUrl+"/access/token/";
/** OAuth CALLback URL*/
public static final String CALLBACK_URL = "yourapp://twitt";
Run Code Online (Sandbox Code Playgroud)
ActivityLogin.java代码片段:
protected void onNewIntent(Intent intent) {
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(AppConfig.CALLBACK_URL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
provider.retrieveAccessToken(consumer, verifier);
Data.OAuthAccessKey = consumer.getToken();
Data.OAuthAccessSecret = consumer.getTokenSecret();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我的连接代码如下所示:
public HttpResponse sampleOauthConnect(String url) throws ...{
/** setup some connection params */
HttpContext context = new BasicHttpContext();
HttpRequestBase request = new HttpGet(url);
if (Data.OAuthConsumer == null)
Data.OAuthConsumer = new CommonsHttpOAuthConsumer(AppConfig.CONSUMER_KEY, AppConfig.CONSUMER_SECRET);
if (Data.OAuthAccessKey == null || Data.OAuthAccessSecret == null)
throw new LoginErrorException(LoginErrorException.NOT_LOGGED_IN);
Data.OAuthConsumer.setTokenWithSecret(Data.OAuthAccessKey, Data.OAuthAccessSecret);
try {
Data.OAuthConsumer.sign(request);
} catch (OAuthMessageSignerException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
} catch (OAuthExpectationFailedException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
} catch (OAuthCommunicationException e) {
throw new LoginErrorException(LoginErrorException.OAUTH_EXCEPTION, e);
}
HttpClient client = new DefaultHttpClient();
/** finally execute this request */
return client.execute(request, context);
}
Run Code Online (Sandbox Code Playgroud)
我可能在复制粘贴过程中遗漏了一些内容,请告诉我这个解决方案是否适合您。我使用的是singpost 1.2.1.1
| 归档时间: |
|
| 查看次数: |
2168 次 |
| 最近记录: |