登录后Twitter4j无法重定向回应用程序

时间:2014-01-11 18:35:04

标签: android twitter access-token

我正在使用twitter4j来获取用户令牌和秘密。 我有一个问题,当Twitter应该在登录后重定向回我的应用程序。浏览器保持在前台,活动永远不会恢复。

以下是清单中的活动声明:

<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();
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试在onCreate()而不是onResume()处理您的回调。

答案 1 :(得分:0)

尝试                          

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="callback"
                android:scheme="x-oauthflow-twitter" />
        </intent-filter>
</activity>

String OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
String OAUTH_CALLBACK_HOST = "callback";
String callBack = OAUTH_CALLBACK_SCHEME+ "://" + OAUTH_CALLBACK_HOST;
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken(callBack);

现在覆盖onNewIntent方法

public void onNewIntent(Intent intent) {
        SharedPreferences prefs = activity.getSharedPreferences(Common.TAG,
                Context.MODE_PRIVATE);
        String callBackScheme = OAUTH_CALLBACK_SCHEME;
        final Uri uri = intent.getData();
        if (uri != null && uri.getScheme().equals(callBackScheme)) {
            new RetrieveAccessTokenTask(prefs).execute(uri);
        }
    }