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

Mat*_*ann 6 c# api response httprequest http-error

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

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());
Run Code Online (Sandbox Code Playgroud)

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

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
}
Run Code Online (Sandbox Code Playgroud)

g7p*_*g7p 1

在 Google API OAuth2 Flow(客户端)页面上查看获取 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 是否是使用 oauth2 auth_code 授权类型获取访问令牌以换取客户端 ID 和客户端密钥的正确资源?

正如其他几个人指出的那样,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) 入门指南,并确保您已按照其中的步骤进行操作;特别是,确保“在 API 列表中,确保 YouTube 数据 API v3 的状态为开启”。如 3b 中所建议。在入门页面上。

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