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

时间:2015-09-29 15:52:28

标签: c# api httprequest response 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());

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



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
}




4 个答案:

答案 0 :(得分:2)

如果您无法在项目中使用Google.Apis.YouTube.v3 Client Library,请使用lib创建测试项目,执行您想要的操作并使用fiddler嗅探流量

答案 1 :(得分:1)

查看在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(Method Not Allowed)响应的原因。

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

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

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

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

另外,请查看https://developers.google.com/youtube/v3/getting-started< - YouTube API(v3)入门指南,确保您已按照其中的步骤操作;特别是,确保"在API列表中,确保YouTube Data API v3的状态为ON。"如3b所示。在入门页面上。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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