2013-03-04 166 views
1

我使用下面的代碼發佈到Twitter的圖像,但我在這條線得到一個錯誤:產生的原因:產生java.io.IOException:無認證的挑戰找到

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

請試試這個找到問題的原因:

  • 確保您在請求傳遞正確的參數。您可以使用其他工具,例如在線客戶端瀏覽器並攔截髮送和接收的請求和響應頭。這樣你將有一組工作參數。
  • 您需要記錄您的Android應用發送/接收的請求和響應頭。如果您使用的是HttpsURLConnection,請使用提供的解決方案here(「複雜工作解決方案」部分中的演示項目)。這樣你就可以看到你實際發送和接收的內容。
  • 您有異常,通常是服務器發送401代碼的結果。要找出原因,您必須執行上述項目。
相關問題