Android应用程序中的Twitter4J不起作用:TwitterException:401

时间:2011-03-03 15:20:13

标签: android twitter twitter4j

我在使用twitter4j时在我的Android应用程序中集成twitter时遇到了问题:

这是我的代码: -------做认证

public void doOauth(){      try {
            String authURL = provider.retrieveRequestToken(consumer,
CALLBACK_URL);
            Log.i("OAuthTwitter", authURL);
            startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(authURL)));
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl(authURL);
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }   }

----关于新意图

    @Override   protected void
 onNewIntent(Intent intent) {
        super.onNewIntent(intent);
                Uri uri = intent.getData();         Log.i(TAG, "uri = "+uri);       if (uri
 != null &&
 uri.toString().startsWith(CALLBACK_URL))
 {          Log.i("OAuthTwitter : url",
 uri.toString());           String verifier =
 uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
            Log.i("OAuthTwitter : verify",
 verifier);             AccessToken a = new
 AccessToken(consumer.getToken(),
 consumer.getTokenSecret());
            Log.i(TAG, "comsumer.toker =
 "+consumer.getToken() );           Log.i(TAG,
 "comsumer.tokersecret =
 "+consumer.getTokenSecret() );             //
 initialize Twitter4J
                        twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY,
 CONSUMER_SECRET);
            twitter.setOAuthAccessToken(a);
                        // create a tweet
                        Date d = new Date(System.currentTimeMillis());
            String tweet = "#OAuth working! " +
 d.toLocaleString();
                        // send the tweet
                        try {
                twitter.updateStatus(tweet);
                Log.i(TAG, "sucess !!!!");          } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();            }
                        // feedback for the user...
                        Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();      }   }

以下是错误消息:

03-03 09:26:34.140: WARN/System.err(4056): twitter4j.TwitterException: 401:Authentication credentials were missing or incorrect.
03-03 09:26:34.140: WARN/System.err(4056): {"request":"\/1\/statuses\/update.json","error":"Request token must be exchanged for an access token before use"}
03-03 09:26:34.140: WARN/System.err(4056):     at twitter4j.http.HttpClient.request(HttpClient.java:318)
03-03 09:26:34.140: WARN/System.err(4056):     at twitter4j.http.HttpClientWrapper.request(HttpClientWrapper.java:66)
03-03 09:26:34.140: WARN/System.err(4056):     at twitter4j.http.HttpClientWrapper.post(HttpClientWrapper.java:97)
03-03 09:26:34.140: WARN/System.err(4056):     at twitter4j.Twitter.updateStatus(Twitter.java:428)
03-03 09:26:34.140: WARN/System.err(4056):     at com.vortexsolution.evenko.ShareByTwitter.onNewIntent(ShareByTwitter.java:83)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1119)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2738)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread.performNewIntents(ActivityThread.java:2750)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2758)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread.access$3100(ActivityThread.java:125)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2080)
03-03 09:26:34.140: WARN/System.err(4056):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 09:26:34.140: WARN/System.err(4056):     at android.os.Looper.loop(Looper.java:123)
03-03 09:26:34.140: WARN/System.err(4056):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 09:26:34.144: WARN/System.err(4056):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 09:26:34.144: WARN/System.err(4056):     at java.lang.reflect.Method.invoke(Method.java:521)
03-03 09:26:34.144: WARN/System.err(4056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-03 09:26:34.144: WARN/System.err(4056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-03 09:26:34.144: WARN/System.err(4056):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

答案就在您的日志中。

03-03 09:26:34.140: WARN/System.err(4056): {"request":"\/1\/statuses\/update.json","error":"Request token must be exchanged for an access token before use"}

获得请求令牌并且用户从允许访问twitter.com返回后,您必须exchange the request token for an access token