无法从Twitter获取访问令牌以访问我在Android中的帐户

时间:2013-11-24 09:26:32

标签: android twitter token

我在我的Android应用程序中使用twitter api在我的帐户上发布应用推文 但是在从twitterb重定向后获取访问令牌时,我无法识别出一些错误。

我希望你能帮助我

这是我的代码

if (!isTwitterLoggedInAlready()) {
                            Toast.makeText(QuoteDialogActivity.this, "token get", Toast.LENGTH_SHORT).show();
                            Uri uri = getIntent().getData();
                            if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
                                // oAuth verifier
                                final String verifier = uri
                                        .getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

                                try {

                                    Thread thread = new Thread(new Runnable(){
                                        @Override
                                        public void run() {
                                            try {

                                                // Get the access token
                                                QuoteDialogActivity.this.accessToken = twitter.getOAuthAccessToken(
                                                        requestToken,verifier);

                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                                    thread.start();

                                    // Shared Preferences
                                    Editor e = mPrefs.edit();

                                    // After getting access token, access token secret
                                    // store them in application preferences
                                    e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                                    e.putString(PREF_KEY_OAUTH_SECRET,
                                            accessToken.getToken());
                                    // Store login status - true
                                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                                    e.commit(); // save changes

                                    Log.e("Twitter OAuth Token", "> " + accessToken.getToken());


                                    // Getting user details from twitter
                                    // For now i am getting his name only
                                    long userID = accessToken.getUserId();
                                    User user = twitter.showUser(userID);
                                    String username = user.getName();

                                    // Displaying in xml ui
//                                  lblUserName.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
                                } catch (Exception e) {
                                    // Check log for login errors
                                    Log.e("Twitter Login Error", "> " + e.getMessage());
                                    e.printStackTrace();
                                }
                            }
                        }           

=============================================== ===

private void loginToTwitter() {
           // Check if already logged in
           if (!isTwitterLoggedInAlready()) {
               ConfigurationBuilder builder = new ConfigurationBuilder();
               builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
               builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
               Configuration configuration = builder.build();

               TwitterFactory factory = new TwitterFactory(configuration);
               twitter = factory.getInstance();


                   Thread thread = new Thread(new Runnable(){
                       @Override
                       public void run() {
                           try {

                               requestToken = twitter
                                       .getOAuthRequestToken(TWITTER_CALLBACK_URL);
                               QuoteDialogActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                       .parse(requestToken.getAuthenticationURL())));

                           } catch (Exception e) {
                               e.printStackTrace();
                           }
                       }
                   });
                   thread.start();         
           } else {
               // user already logged into twitter
               Toast.makeText(getApplicationContext(),
                       "Already Logged into twitter", Toast.LENGTH_LONG).show();
           }
       }


     private boolean isTwitterLoggedInAlready() {
           // return twitter login status from Shared Preferences
           return mPrefs.getBoolean(PREF_KEY_TWITTER_LOGIN, false);
       }

=============================================== =====

logcat的

11-24 10:32:29.104: E/Twitter Login Error(3867): > null
11-24 10:32:29.104: W/System.err(3867): java.lang.NullPointerException
11-24 10:32:29.104: W/System.err(3867):     at com.engahmedphp.successquotes.MainActivity.onCreate(MainActivity.java:143)
11-24 10:32:29.104: W/System.err(3867):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-24 10:32:29.104: W/System.err(3867):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
11-24 10:32:29.104: W/System.err(3867):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
11-24 10:32:29.104: W/System.err(3867):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
11-24 10:32:29.104: W/System.err(3867):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
11-24 10:32:29.104: W/System.err(3867):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 10:32:29.104: W/System.err(3867):     at android.os.Looper.loop(Looper.java:123)
11-24 10:32:29.114: W/System.err(3867):     at android.app.ActivityThread.main(ActivityThread.java:4633)
11-24 10:32:29.114: W/System.err(3867):     at java.lang.reflect.Method.invokeNative(Native Method)
11-24 10:32:29.114: W/System.err(3867):     at java.lang.reflect.Method.invoke(Method.java:521)
11-24 10:32:29.114: W/System.err(3867):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-24 10:32:29.114: W/System.err(3867):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-24 10:32:29.114: W/System.err(3867):     at dalvik.system.NativeStart.main(Native Method)
11-24 10:32:29.114: W/System.err(3867): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-24 10:32:29.114: W/System.err(3867):     at android.os.Handler.<init>(Handler.java:121)
11-24 10:32:29.114: W/System.err(3867):     at android.widget.Toast.<init>(Toast.java:68)
11-24 10:32:29.114: W/System.err(3867):     at android.widget.Toast.makeText(Toast.java:231)
11-24 10:32:29.114: W/System.err(3867):     at com.engahmedphp.successquotes.MainActivity$5.run(MainActivity.java:126)
11-24 10:32:29.114: W/System.err(3867):     at java.lang.Thread.run(Thread.java:1096)

0 个答案:

没有答案