简体   繁体   English

发送okhttp请求时:HTTP错误405和invalid_client

[英]When sending okhttp request: HTTP ERROR 405 and invalid_client

I'm making a request to a website. 我正在向网站提出请求。 However, I keep getting a returned JSON of {"error":"invalid_client"} . 但是,我不断收到返回的JSON {"error":"invalid_client"} Additionally, when I navigate to the URL I'm making the request to through a web browser it shows HTTP ERROR 405 . 另外,当我导航到URL时,我正在通过Web浏览器发出请求,它显示HTTP ERROR 405

From what I read on those errors that might mean that my request isn't structured correctly. 根据我读到的那些错误,可能意味着我的请求结构不正确。

According to the API's documentation, this is an example of the request type I'm trying to do: 根据API的文档,这是我尝试执行的请求类型的示例:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "client_secret={your_client_secret}&client_id={your_client_id}&code={your_authorization_code}&grant_type=authorization_code&redirect_uri={your_redirect_uri}");
Request request = new Request.Builder()
  .url("https://api.website.com/v2/oauth2/token")
  .post(body)
  .addHeader("content-type", "application/x-www-form-urlencoded")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

From what I can tell mine should be doing the same thing, just a little differently. 据我所知,我应该做同样的事情,只是有所不同。


Here is a Pastebin of my doInBackground method (I'm using AsynchTask). 是我的doInBackground方法的doInBackground (我正在使用AsynchTask)。 Here is the more applicable part: 这是更适用的部分:

OkHttpClient client = new OkHttpClient();

// A section here gets strings from a JSON file storing values such as client_id

 RequestBody bodyBuilder = new FormBody.Builder()
  .add("client_secret", CLIENT_SECRET)
  .add("client_id", CLIENT_ID)
  .add("code", AUTHORIZATION_CODE)
  .add("grant_type", GRANT_TYPE)
  .add("redirect_uri", REDIRECT_URI)
  .build();
 System.out.println("Built body: " + bodyBuilder.toString());

 String mediaTypeString = "application/x-www-form-urlencoded";
 MediaType mediaType = MediaType.parse(mediaTypeString);
 RequestBody body = RequestBody.create(mediaType, requestbodyToString(bodyBuilder)); // See Edit 1

 Request request = new Request.Builder()
  .url(TARGET_URL)
  .post(body)
  .addHeader("content-type", mediaTypeString)
  .addHeader("cache-control", "no-cache")
  .build();

 try {
  System.out.println("Starting request.");
  Response response = client.newCall(request).execute();
  String targetUrl = request.url().toString() + bodyToString(request);
  System.out.println("request: " + targetUrl);
  String responseBodyString = response.body().string();
  System.out.println("response: " + responseBodyString);
  return responseBodyString;
 } catch (IOException ex) {
  System.out.println(ex);
 }

Like I said, I keep getting a returned JSON of {"error":"invalid_client"} , and when I navigate to the URL I'm making the request to through a web browser it shows HTTP ERROR 405 . 就像我说的那样,我不断收到返回的JSON {"error":"invalid_client"} ,当我导航到URL时,我正在通过网络浏览器发出请求,它显示HTTP ERROR 405


I'd love to provide as much additional information as you need. 我很乐意提供您需要的更多信息。 Thanks! 谢谢!


Edit 1: The second parameter of this used to be "bodyBuilder.toString()", but I changed it because I realized it wasn't actually sending the body. 编辑1:此参数的第二个参数曾经是“ bodyBuilder.toString()”,但是我更改了它,因为我意识到它实际上并不是在发送主体。 The result is still the same - {"error":"invalid_client"} . 结果仍然相同- {"error":"invalid_client"} The method now used comes from here . 现在使用的方法来自这里

I figured out what it was - I hadn't actually been writing the authentication_code to the file, only adding it to another JSONObject. 我弄清楚它是什么-我实际上并没有将authentication_code写入文件,只是将其添加到另一个JSONObject中。 Oops. 哎呀。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM