引起:java.io.IOException:未发现身份验证质询

时间:2013-03-04 07:00:12

标签: android scribe android-twitter

我使用下面的代码将图片发布到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

1 个答案:

答案 0 :(得分:1)

请尝试这样找出问题的原因:

  • 确保在请求中传递正确的参数。您可以使用其他工具,例如浏览器中的在线客户端以及拦截发送和接收的请求和响应头。通过这种方式,您将拥有一组工作参数。
  • 您需要记录Android应用发送/接收的请求和响应标头。如果您正在使用HttpsURLConnection,请使用提供的解决方案here(“复杂工作解决方案”部分中的演示项目)。通过这种方式,您可以看到实际发送和接收的内容。
  • 您拥有的例外情况,通常是服务器发送401代码的结果。要查找原因,您必须执行上述项目。