gpt4 book ai didi

android - 由 : java. io.IOException 引起:未找到身份验证挑战

转载 作者:行者123 更新时间:2023-11-30 03:44:36 26 4
gpt4 key购买 nike

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

最佳答案

请尝试找出问题的原因:

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

关于android - 由 : java. io.IOException 引起:未找到身份验证挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15196188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com