1

我在 android 应用程序中使用 twitter4j。我正在使用带有我的消费者密钥和秘密的代码,当我登录到 twitter 时,它在第一次尝试时成功登录。但是当我再次运行代码然后尝试时,因为我之前已经过身份验证,它让我登录但随后给出“获取访问令牌时出错”。我不明白如何处理它..有人可以帮忙..我正在使用此代码登录

    public class TwitterLoginActivity extends Activity {
        TextView tv;
        Button loginBtn;

        private TwitterApp mTwitter;
        private ConnectionDetector cd;
        ProgressDialog pDialog;
        private static SharedPreferences mSharedPreferences;
     AlertDialogManager alert = new AlertDialogManager();
        RequestToken requestToken;
        private static final String CONSUMER_KEY = "*****************";
        private static final String CONSUMER_SECRET = "*****************************************";

        static String PREFERENCE_NAME = "twitter_oauth";
      static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
      static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
      static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";


      static final String URL_TWITTER_AUTH = "auth_url";
      static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
      static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";

      public void storeAccessToken(AccessToken aToken)   {
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putString(PREF_KEY_OAUTH_TOKEN, aToken.getToken());
            editor.putString(PREF_KEY_OAUTH_SECRET, aToken.getTokenSecret());
            editor.commit();}
        private enum FROM {
            TWITTER_POST, TWITTER_LOGIN
        };

        private enum MESSAGE {
            SUCCESS, DUPLICATE, FAILED, CANCELLED
        };

        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            setContentView(R.layout.twitter_login);
            super.onCreate(savedInstanceState);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            mTwitter = new TwitterApp(this, CONSUMER_KEY, CONSUMER_SECRET);
            requestToken = mTwitter.getOAuthRequestToken;

            tv = (TextView) findViewById(R.id.welcome_txt);
            loginBtn = (Button) findViewById(R.id.login_btn);
            loginBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mTwitter.setListener(mTwLoginDialogListener);
                    mTwitter.resetAccessToken();
                    if (mTwitter.hasAccessToken() == true) {
                        postAsToast(null, null);

                    }
                    else {
                        mTwitter.authorize();
                    }

                }
            });
        }

        private void postAsToast(FROM twitterPost, MESSAGE success) {
            switch (twitterPost) {
            case TWITTER_LOGIN:
                switch (success) {
                case SUCCESS:
                    Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG).show();

                    break;
                case FAILED:
                    Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
                default:
                    break;
                }
                break;

            }
        }


        private TwDialogListener mTwLoginDialogListener = new TwDialogListener() {

            public void onError(String value) {
                postAsToast(FROM.TWITTER_LOGIN, MESSAGE.FAILED);
                Log.e("TWITTER", value);
                mTwitter.resetAccessToken();
            }

            public void onComplete(String value) {

                Intent intent = new Intent(TwitterLoginActivity.this,
                        TweetsListActivity.class);
                startActivity(intent);
                finish();
            }

        };
    }

i have also used AlertDialogManager.java,ConnectionDetector.java classes in right manner. please help
4

1 回答 1

7

参考这个链接:

http://www.androidhive.info/2012/09/android-twitter-oauth-connect-tutorial

如果有任何疑问,请告诉我。

它工作正常......只需使用最新的 jar 文件就可以了!

编辑: 只需在 oncreate off Activity 中添加以下代码:

 if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }
于 2013-08-22T11:32:31.517 回答