我如何使用OAuth在Android中回电?
-
09-10-2019 - |
题
从过去的5天开始,我正在使用Oauth搜索Android的Twitter的最佳代码工作,我发现了很多。但是没有一个1的运行完美。我如何获得一些代码。它的工作正常,但我有问题。我想在字符串回调=“?”中必须写的是什么?这样我的Android浏览器将我重定向到我的应用程序,而不是在身份验证后呆在那里..如果我不使用回调并使用 oauth.out_of_band 然后,浏览器显示PIN代码和剂量,而不是将浏览器重定向到我的应用程序。请帮助我让我知道我做错了什么。
这里我的代码走了
package com.example.tweeter;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class Tweeter extends Activity {
private String CONSUMER_KEY = "CONSUMER_KEY";
private String CONSUMER_SECRET = "CONSUMER_SECRET";
private static final Uri CALLBACK_URI = Uri.parse("PicPuzzle://tkxel");
private String CALLBACK_URL = "PicPuzzle://tkxel";
OAuthProvider provider ;
CommonsHttpOAuthConsumer consumer ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
consumer = new CommonsHttpOAuthConsumer(
CONSUMER_KEY, CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(
"http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
provider.setOAuth10a(true);
HttpClient client = new DefaultHttpClient();
String authUrl = "http://www.yahoo.com";
try {
//This line work perfect but it not redirect me to my application
authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
//I want to send a calll back but It give exception
///*** authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URI.toString());
} 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();
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
}
@Override
protected void onResume() {
// this must be places in activity#onResume()
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter("oauth_verifier");
// this will populate token and token_secret in consumer
try {
provider.retrieveAccessToken(consumer, 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();
}
}
super.onResume();
}
}
和我的 androidManifest.xml 看起来像这样
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
>
<activity
android:name=".Tweeter"
android:label="@string/app_name"
>
<intent-filter>
<action
android:name="android.intent.action.MAIN"/>
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<intent-filter>
<action
android:name="android.intent.action.VIEW"
></action>
<category
android:name="android.intent.category.DEFAULT"
></category>
<category
android:name="android.intent.category.BROWSABLE"
></category>
<data
android:scheme="PicPuzzle"
android:host="tkxel"
></data>
</intent-filter>
</application>
<uses-sdk
android:minSdkVersion="7"/>`enter code here`
解决方案
回调URL应基于您在清单中为活动配置的内容。看起来您正在使用 scheme="PicPuzzle"
, host="tkxel"
. 。所以您的回调URL是 PicPuzzle://tkxel
我觉得 <data>
标签应在特定上 Activity
不过,您想接收回调(看起来您目前在整个应用程序上都有它)。
不隶属于 StackOverflow