代码之家  ›  专栏  ›  技术社区  ›  Nidhi

在twitter安卓应用程序中获取访问令牌时出错

  •  1
  • Nidhi  · 技术社区  · 11 年前

    我在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
    
    1 回复  |  直到 11 年前
        1
  •  7
  •   albfan    10 年前

    请参阅此链接:

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

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

    它很好用。。。只需使用最新的jar文件即可!!

    编辑: 只需在oncreate-off活动中添加以下代码:

     if (android.os.Build.VERSION.SDK_INT > 9) {
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
            }