Twitter4J in android app don't work:TwitterException: 401
I've got a problem while integrating twitter in my android application while using twitter4j:
This is my code: ------- do auth
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();
} }
---- on new intent
@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(); } }
Here is the error message:
03-03 09:26:34.140: WARN/System.err(4056): twitter4j.TwitterException: 401:Authentication credentials wer开发者_Python百科e 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)
The answer is right in your logs.
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"}
After you get a request token and the user returns from allowing access on twitter.com you have to exchange the request token for an access token.
精彩评论