当我使用 scribe 库将图像发布到 twitter 时,出现以下异常
org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '<?xml version="1.0" encoding="UTF-8"?
我正在使用 scribe1.3.3 库。
我在谷歌搜索这个异常,但没有找到任何解决方案。
源代码:
OAuthService oAuth = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey("6JyIkj71ZqG4wk3YF0Y4hw") // REPLACE WITH YOUR OWN!!!
.apiSecret("sJl9aRVqlEt7nxlKvpMVK6tLULz5FSQ2KUOW0yie4") // REPLACE WITH YOUR OWN!!!
.callback(TwitterDialog.redirectedurl) // REPLACE WITH YOUR OWN!!!
.build();
Uri uri = Uri.parse(TwitterDialog.redirectedurl);
Token requestToken = new Token( uri.getQueryParameter("oauth_token"),uri.getQueryParameter("oauth_verifier"));
Verifier verifier = new Verifier(uri.getQueryParameter("oauth_verifier"));
if(verifier!=null){
Log.e("verifier created","created");
}
Token accesstoken=oAuth.getAccessToken(requestToken, verifier);
Log.e("raw resp", accesstoken.getRawResponse());
if(accesstoken!=null){
Log.e("hello","hhello");
Log.e("access token","hi"+accesstoken.toString());
OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json");
oAuth.signRequest(accesstoken, request); // ENTER USER'S ACCESS TOKEN
// ADD MULTIPART FORM
try
{
MultipartEntity entity = new MultipartEntity();
entity.addPart("status", new StringBody("insert vacuous statement here")); // THIS IS THE TWITTER MESSAGE
entity.addPart("media", new FileBody(new File("/storage/sdcard0/HelloAlbum/1361964305003.jpg"))); // THIS IS THE PHOTO TO UPLOAD
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
request.addPayload(out.toByteArray());
request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
}
catch (UnsupportedEncodingException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
// SEND REQUEST
try {JSONObject response = new JSONObject (request.send().getBody());}
catch (JSONException e) {Log.e("TOUR_APP_TAG", "JSONException Thrown: " + e.getMessage());}
}
日志输出:
03-05 15:52:24.810: E/AndroidRuntime(28777): FATAL EXCEPTION: main
03-05 15:52:24.810: E/AndroidRuntime(28777): org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '<?xml version="1.0" encoding="UTF-8"?>
03-05 15:52:24.810: E/AndroidRuntime(28777): <hash>
03-05 15:52:24.810: E/AndroidRuntime(28777): <request>/oauth/access_token</request>
03-05 15:52:24.810: E/AndroidRuntime(28777): <error>Invalid / expired Token</error>
03-05 15:52:24.810: E/AndroidRuntime(28777): </hash>
03-05 15:52:24.810: E/AndroidRuntime(28777): '
03-05 15:52:24.810: E/AndroidRuntime(28777): at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
03-05 15:52:24.810: E/AndroidRuntime(28777): at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
03-05 15:52:24.810: E/AndroidRuntime(28777): at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:103)
03-05 15:52:24.810: E/AndroidRuntime(28777): at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:84)
03-05 15:52:24.810: E/AndroidRuntime(28777): at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:89)
03-05 15:52:24.810: E/AndroidRuntime(28777): at com.android.twitter.TwitterActivity$1.onComplete(TwitterActivity.java:317)
03-05 15:52:24.810: E/AndroidRuntime(28777): at com.twitter.android.TwitterApp$1.handleMessage(TwitterApp.java:236)
03-05 15:52:24.810: E/AndroidRuntime(28777): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 15:52:24.810: E/AndroidRuntime(28777): at android.os.Looper.loop(Looper.java:137)
03-05 15:52:24.810: E/AndroidRuntime(28777): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-05 15:52:24.810: E/AndroidRuntime(28777): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 15:52:24.810: E/AndroidRuntime(28777): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 15:52:24.810: E/AndroidRuntime(28777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-05 15:52:24.810: E/AndroidRuntime(28777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-05 15:52:24.810: E/AndroidRuntime(28777): at dalvik.system.NativeStart.main(Native Method)