2
我使用twitter4j獲取用戶令牌和祕密。 我在登錄後應用程序重定向到我的應用程序時遇到問題。瀏覽器停留在前臺,活動永遠不會恢復。Twitter4j無法在登錄後重定向迴應用程序
這裏是清單中的活性聲明:
<activity
android:name=".TwitterActivity"
android:screenOrientation="portrait" >
<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" />
</intent-filter>
</activity>
我已經把這些常量:
private static final String TWITTER_CALLBACK_URL = "myapp:///";
private static final String TWITTER_OAUTH_VERIFIER_URL = "oauth_verifier";
當登錄按鈕,用戶點擊:
ConfigurationBuilder configBuilder = new ConfigurationBuilder();
configBuilder.setOAuthConsumerKey(getString(R.string.twitter_consumer_key));
configBuilder.setOAuthConsumerSecret(getString(R.string.twitter_consumer_secret));
mTwitter = new TwitterFactory(configBuilder.build()).getInstance();
mProgressDialog.show();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
mTwitterRequestToken = mTwitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mProgressDialog.dismiss();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mTwitterRequestToken.getAuthenticationURL())));
}
}.execute();
在onResume
方法,我檢查意圖並檢索令牌和祕密
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
String verifier = uri.getQueryParameter(TWITTER_OAUTH_VERIFIER_URL);
try {
mTwitterToken = mTwitter.getOAuthAccessToken(mTwitterRequestToken, verifier);
Toast.makeText(this, "token: " + mTwitterToken.getToken() + "\nSecret: " + mTwitterToken.getTokenSecret(), Toast.LENGTH_SHORT).show();
} catch (TwitterException e) {
e.printStackTrace();
}
}