Http 请求状态代码 405 ReasonPhrase:'' Youtube APi

Http Request Status Code 405 ReasonPhrase: '' Youtube APi

我尝试发送这两个请求,但我只收到标题中列出的错误作为响应。 我对 google 服务器的两个网络请求是这样的:

HttpClient http = new HttpClient();

HttpResponseMessage response = await http.GetAsync("https://accounts.google.com/o/oauth2/token?code="+localSettings.Values["AccessKey"]+"&client_id=XX-XXXXXXXXXX.apps.googleusercontent.com&client_secret=XXXXX-XXXX&redirect_uri=http://localhost/oauth2callback&grant_type=authorization_code");

//response.EnsureSuccessStatusCode();
Debug.WriteLine(response.ToString());
HttpResponseMessage response1 = await http.GetAsync("https://content.googleapis.com/youtube/v3/subscriptions?part=id&maxResults=10&mine=true&key="+localSettings.Values["AccessKey"]);
Debug.WriteLine(response1.ToString());

我从调试器得到以下输出:

StatusCode: 405, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.StreamContent, Headers:
{
  server: GSE
  alt-svc: quic=":443"; p="1"; ma=604800
  cache-control: max-age=0, private
  accept-ranges: none
  date: Tue, 29 Sep 2015 16:05:03 GMT
  x-frame-options: SAMEORIGIN
  vary: Accept-Encoding
  x-content-type-options: nosniff
  alternate-protocol: 443:quic,p=1
  x-xss-protection: 1; mode=block
  content-type: application/json
  expires: Tue, 29 Sep 2015 16:05:03 GMT
}
StatusCode: 400, ReasonPhrase: '', Version: 2.0, Content: System.Net.Http.StreamContent, Headers:
{
  server: GSE
  alt-svc: quic=":443"; p="1"; ma=604800
  cache-control: max-age=0, private
  accept-ranges: none
  date: Tue, 29 Sep 2015 16:05:04 GMT
  x-frame-options: SAMEORIGIN
  vary: X-Origin
  vary: Origin
  vary: Accept-Encoding
  x-content-type-options: nosniff
  alternate-protocol: 443:quic,p=1
  x-xss-protection: 1; mode=block
  content-type: application/json; charset=UTF-8
  expires: Tue, 29 Sep 2015 16:05:04 GMT
}

如果您可以为 localSettings.Values["AccessKey"] 提供导致错误的值,那将会有所帮助。 我的猜测是这个变量包含应该编码的字符。 你评论说 localSettings.Values["AccessKey"] 可能是 X/XXXXX。如果结果 url 类似于 /token?code=X/XXXXX"...,您可以尝试 /token?code=X%2FXXXXX

您的第一个请求应该是 HTTP POST 调用。目前您正在使用 GET。 GET 似乎导致错误 405,这表明不允许使用 GET 方法。

在 Google API OAuth2 流程(客户端)页面查看获​​取 oauth2 访问令牌的详细信息 - https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps#Obtaining_Access_Tokens

您会注意到获取访问令牌的资源是 o/oauth2/auth 而不是 o/oauth2/token(这似乎是您使用的代码片段)- 这应该是服务器返回 405(方法不允许)响应的原因。

在您使用的 google api 版本中,您能否检查 o/oauth2/token 是否是获取访问令牌以换取客户端 ID 和客户端密码的正确资源使用 oauth2 auth_code 授权类型?

正如其他一些人指出的那样,405 响应表明所请求的资源不允许使用该方法。在我看来,在 API 版本 3 中 GET 的正确资源是 o/oauth2/auth。

查看您的代码片段,在我看来,您正在向 o/oauth2/token 资源发送 GET 请求,通过您的 "AccessKey"、client_id 和 client_secret 和 oauth2 grant_type=auth_code 并期望取回访问令牌和刷新令牌;并且您希望使用由此收到的访问令牌来发出下一个 GET 请求。

但是,如前所述,o/oauth2/token 似乎不是在 Youtube API v3 中获取访问令牌的正确资源(假设您正尝试在 v3 上进行此操作)

此外,请查看 https://developers.google.com/youtube/v3/getting-started <-- YouTube API (v3) 入门指南并确保您已按照其中的步骤进行操作;特别是,确保 "In the list of APIs, make sure the status is ON for the YouTube Data API v3." 如 3b 中所建议的那样。在入门页面上。

如果您没有使用(/尝试使用)API 的 v3,请告诉我。

如果不能使用Google.Apis.YouTube.v3 Client Library in your project, create a test project with the lib, doing what do you want and sniff the traffic with fiddler