Caused by: java.io.IOException: No authentication challenges found

user1891910 picture user1891910 · Mar 4, 2013 · Viewed 6.9k times · Source

I am using the code below to post an image to Twitter, but I am getting an error at this line:

Token accesstoken=oAuth.getAccessToken(requestToken, verifier);

I searched for the error but didn't find any solution. I want to know why this error is occurring, and what I am doing wrong.

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 output:

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

Answer

ivan_onys picture ivan_onys · Apr 6, 2013

Please, try this to find the cause of the problem:

  • Make sure that you pass correct parameters in request. You may use another tool e.g. online client in browser and intercept request and response headers sent and received. This way you'll have a set of working parameters.
  • You need logging for request and response headers sent / received by your Android app. If you are using HttpsURLConnection, please, use solution provided here (demo project in "Complex working solution" section). This way you can see, what you are actually sending and receiving.
  • Exception you have, is usually result of server sending 401 code. To find the cause you have to perform items above.