我使用下面的代码将图片发布到Twitter,但我在这一行收到错误:
Token accesstoken=oAuth.getAccessToken(requestToken, verifier);
我搜索了错误,但没有找到任何解决方案。我想知道为什么会出现这种错误,以及我做错了什么。
OAuthService oAuth = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey("6JyIkj71ZqG4wk3YF0Y4hw") // REPLACE WITH YOUR OWN!!!
.apiSecret("sJl9aRVqlEt7nxlKvpMVK6tLULz5FSQ2KUOW0yie4") // REPLACE WITH YOUR OWN!!!
.callback(TwitterApp.CALLBACK_URL) // REPLACE WITH YOUR OWN!!!
.build();
Log.e("hi", "hi");
Token requestToken = oAuth.getRequestToken();
if (requestToken != null) {
Log.e("has requestToken", "has");
}
String authUrl = oAuth.getAuthorizationUrl(requestToken);
Verifier verifier = new Verifier(getVerifier(authUrl));
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());
}
}
Logcat输出:
03-04 12:24:11.645: D/dalvikvm(2287): WAIT_FOR_CONCURRENT_GC blocked 15ms
03-04 12:24:12.315: E/AndroidRuntime(2287): FATAL EXCEPTION: main
03-04 12:24:12.315: E/AndroidRuntime(2287): org.scribe.exceptions.OAuthException: Problems while creating connection.
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.model.Request.send(Request.java:70)
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:81)
03-04 12:24:12.315: E/AndroidRuntime(2287): at com.android.twitter.TwitterActivity$1.onComplete(TwitterActivity.java:298)
03-04 12:24:12.315: E/AndroidRuntime(2287): at com.twitter.android.TwitterApp$1.handleMessage(TwitterApp.java:244)
03-04 12:24:12.315: E/AndroidRuntime(2287): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 12:24:12.315: E/AndroidRuntime(2287): at android.os.Looper.loop(Looper.java:137)
03-04 12:24:12.315: E/AndroidRuntime(2287): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-04 12:24:12.315: E/AndroidRuntime(2287): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 12:24:12.315: E/AndroidRuntime(2287): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 12:24:12.315: E/AndroidRuntime(2287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 12:24:12.315: E/AndroidRuntime(2287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 12:24:12.315: E/AndroidRuntime(2287): at dalvik.system.NativeStart.main(Native Method)
03-04 12:24:12.315: E/AndroidRuntime(2287): Caused by: java.io.IOException: No authentication challenges found
03-04 12:24:12.315: E/AndroidRuntime(2287): at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:436)
03-04 12:24:12.315: E/AndroidRuntime(2287): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:416)
03-04 12:24:12.315: E/AndroidRuntime(2287): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:365)
03-04 12:24:12.315: E/AndroidRuntime(2287): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:301)
03-04 12:24:12.315: E/AndroidRuntime(2287): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.model.Response.<init>(Response.java:28)
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.model.Request.doSend(Request.java:110)
03-04 12:24:12.315: E/AndroidRuntime(2287): at org.scribe.model.Request.send(Request.java:62)
03-04 12:24:12.315: E/AndroidRuntime(2287): ... 12 more
答案 0 :(得分:1)
请尝试这样找出问题的原因: