我有以下功能来管理登录:
private static final String CALLBACK_URL = "myapp://twitter";
private void loginViaOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
provider.setOAuth10a(true);
String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
} catch (Exception e) {
e.printStackTrace();
}
}
和Manifest.xml
<activity android:name=".Twitterconnect"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<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="myapp" android:host="twitter" />
</intent-filter>
</activity>
我得到的回应是:
05-03 11:24:03.968: INFO/ActivityManager(59): Starting activity: Intent {
act=android.intent.action.VIEW
cat=[android.intent.category.BROWSABLE]
dat=myapp://twitter?oauth_token=tJpJHOOwoTGMwdvHyYbfX2tyHKOp0Y2kdRRZf3sM&oauth_verifier=xc49oM8eVVmK46ZSLz2RMT2uqXn3SxrMxf5ZAMXaD2Y
cmp=my.app/.Twitterconnect
}
这是我的OnResume功能:
@Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d("verifier:", verifier);
try {
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, verifier);
ACCESS_KEY = consumer.getToken();
ACCESS_SECRET = consumer.getTokenSecret();
AccessToken a = new AccessToken(ACCESS_KEY, ACCESS_SECRET);
// initialize Twitter4J
twitter = new Twitter();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
Log.e("TWITTER", "step1");
twitter.updateStatus("Just shared the ".concat(tweet).concat(" on Twitter!"));
Log.e("TWITTER", ""+buttonTwitterShare.getVisibility());
buttonTwitterShare.setVisibility(View.GONE);
Log.e("TWITTER", "step2");
Log.e("TWITTER", ""+buttonTwitterShare.getVisibility());
Toast.makeText(getBaseContext(), "Post successfully tweeted!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的问题是为什么无论我使用通过Uri uri = this.getIntent().getData();
获取的URI登录的配置文件始终为NULL?
如何从回复中提取oauth_token
和oauth_verifier
:dat=myapp://twitter?oauth_token=tJpJHOOwoTGMwdvHyYbfX2tyHKOp0Y2kdRRZf3sM&oauth_verifier=xc49oM8eVVmK46ZSLz2RMT2uqXn3SxrMxf5ZAMXaD2Y
?
答案 0 :(得分:1)
这可能适合你
Uri uri = this.getIntent().getData();
if (uri == null) {
return null;
}
String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");
即便是这两行
String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");
正如你已经得到的那样,uri应该这样做。