4

我在 android 中使用 Twitter4j API,当我从 twitter 浏览器屏幕按后退按钮取消授权时,它带我回到 PrepareRequestTokenActivity 的 onResume() (由 twitter4j api 提供的活动),我正在从 onResume 完成此活动,但是当授权成功,然后它也将转到 onResume() ,然后转到同一活动的 onNewIntent() 方法,但活动从恢复完成并且身份验证失败。有什么方法可以从 twitter 浏览器屏幕捕获后退按钮?下面是活动。

public class PrepareRequestTokenActivity extends Activity {



        public static final String CONSUMER_KEY = "Hr8aDOFeDdY9UbvQB0w2w";
        public static final String CONSUMER_SECRET= "wfZOJYkYVEYrmdmltOaKfRdnUfSiUkr2MQdjRUY2xU";

        public static final String REQUEST_URL = "http://twitter.com/oauth/request_token"; //"https://api.twitter.com/oauth/request_token"
        public static final String ACCESS_URL = "http://twitter.com/oauth/authorize"; //"https://api.twitter.com/oauth/authorize"
        public static final String AUTHORIZE_URL = "http://twitter.com/oauth/access_token"; //"https://api.twitter.com/oauth/access_token"

        final public static String OAUTH_CALLBACK_SCHEME = "droidnotify-oauth-twitter";
        final public static String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://callback";

        private boolean _debug = false;
    private OAuthConsumer _consumer; 
    private OAuthProvider _provider;


        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                _debug = Log.getDebug();
                if (_debug) Log.v("PrepareRequestTokenActivity.onCreate()");
        try {
                _consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            //_provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
            _provider = new DefaultOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
        } catch (Exception ex) {
                if (_debug) Log.e("PrepareRequestTokenActivity.onCreate() Error creating consumer / provider: " + ex.toString());
                }
        if (_debug) Log.v("PrepareRequestTokenActivity.onCreate() Starting task to retrieve request token.");
                new OAuthRequestTokenTask(this, _consumer, _provider).execute();
        }


        @Override
        public void onNewIntent(Intent intent) {
                super.onNewIntent(intent); 
                if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent()");
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                final Uri uri = intent.getData();
                if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
                        if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent() Callback received : " + uri);
                        if (_debug) Log.v("PrepareRequestTokenActivity.onNewIntent() Retrieving Access Token");
                        new RetrieveAccessTokenTask(this, _consumer, _provider, prefs).execute(uri);
                        finish();       
                }
        }


        public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {

                private Context _context;
                private OAuthProvider _provider;
                private OAuthConsumer _consumer;
                private SharedPreferences _prefs;


                public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider, SharedPreferences prefs) {
                        _context = context;
                        _consumer = consumer;
                        _provider = provider;
                        _prefs=prefs;
                }


                @Override
                protected Void doInBackground(Uri...params) {
                        final Uri uri = params[0];
                        final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
                        try {
                                _provider.retrieveAccessToken(_consumer, oauth_verifier);
                                final Editor edit = _prefs.edit();
                                edit.putString(OAuth.OAUTH_TOKEN, _consumer.getToken());
                                edit.putString(OAuth.OAUTH_TOKEN_SECRET, _consumer.getTokenSecret());
                                edit.commit();
                                String token = _prefs.getString(OAuth.OAUTH_TOKEN, "");
                                String secret = _prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
                                _consumer.setTokenWithSecret(token, secret);
                                //_context.startActivity(new Intent(_context, AndroidTwitterSample.class));
                                //executeAfterAccessTokenRetrieval();
                                Toast.makeText(_context, "Twitter Authentication Successfull", Toast.LENGTH_LONG);
                                Toast.makeText(_context, "OAuth.OAUTH_TOKEN KEY: " + OAuth.OAUTH_TOKEN + ", OAuth.OAUTH_TOKEN Value: " + _consumer.getToken(), Toast.LENGTH_LONG);
                                Toast.makeText(_context, "OAuth.OAUTH_TOKEN_SECRET KEY: " + OAuth.OAUTH_TOKEN_SECRET + ", OAuth.OAUTH_TOKEN_SECRET Value: " + _consumer.getTokenSecret(), Toast.LENGTH_LONG);
                                if (_debug) Log.v("OAuth - Access Token Retrieved");

                        } catch (Exception ex) {
                                if (_debug) Log.e("OAuth - Access Token Retrieval Error: " + ex.toString());
                        }
                        return null;
                }


        }       

}
4

2 回答 2

2

只需将一个布尔标志初始化为 false 使其在 PrepareRequestTokenActivity 的 onNewIntent() 函数中为真,并在 onResume 函数中添加一个 if 条件,如果此标志为假则完成活动,如果为真则不执行任何操作,即当您单击返回按钮时从浏览器直接转到onResume,此时您的标志将为false,然后此活动将完成,当成功或不,谢谢,它将首先转到onNewIntent()然后onResume,所以如果为true,则活动将不会完成

于 2012-07-27T13:02:20.723 回答
1

使用下面的代码作为 CALLBACK_URL &如果您对此有任何疑问,请告诉我。

public static final String  OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String  OAUTH_CALLBACK_HOST = "callback";
public static final String  CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
于 2012-07-26T10:18:13.133 回答