Http Request Status Code 405 ReasonPhrase: '' Youtube APi
Asked Answered
S

4

6

I try to send this two requests but I only get as a response the errors listed in the title. My two webrequests to the google servers are like this:

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());

I get the following output from the Debugger:

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
}
Swiger answered 29/9, 2015 at 15:52 Comment(18)
Could you post the full error message please?Alexisaley
@Alexisaley updated post with debug outputSwiger
Are the URLs that you're trying to access valid?Alexisaley
@Alexisaley I don't really know I'm confused by the chance from youtube api 2.0 to 3.0: Here is the guide from google: developers.google.com/youtube/2.0/… and I don't know what I did wrong it seems that I'm using the right pattern. Step 5 is the exchange authorization code for refresh and access tokens which isn't working for me.Swiger
You have a reference to localSettings.Values["accessKey"] in the URLs - is this value correct if you print it out?Alexisaley
@Alexisaley Yes I just checked it it has the right format like X/XXXXXSwiger
For the 2nd URL, it's possible that you don't need to include content in the URL - try just using https://www.googleapis.com/youtube/v3....Alexisaley
Can you explain exactly what you're trying to achieve with each of your two requests? Also, are you trying to migrate from API v2.0 to v3.0?Alexisaley
@Alexisaley I get the acesscode for the api there is no problem and then i want to exchange this access code for refresh and access tokens which doesn't work and the last reqeust is for getting the list of subscribed channelsSwiger
@Alexisaley There is no difference between the api version 2.0 and 3.0 of how to Exchange authorization code for refresh and access tokens but it won't work . developers.google.com/youtube/v3/guides/auth/installed-appsSwiger
Try escaping the " characters in the URLs by prefixing them with a backslash: \".Alexisaley
@Alexisaley Can you give me a example for the first webrequest - so I understand how you imagine itSwiger
My mistake - the speech marks aren't part of the URL string. For the second URL, you could try "https://googleapis.com/youtube/v3/subscriptions?part=id&maxResults=10&mine=true&key="+localSettings.Values["AccessKey"].Alexisaley
@Alexisaley That's only changing the status code to 404 :DSwiger
It was worth a try. :P Is there any way you can try to find out more about the errors? Maybe step through with a debugger? Other than that, I'm out of ideas, sorry.Alexisaley
Try setting an user agent header. I think i remember i had "problems" with google apis when it is not set. (Even it´s not documented)Nysa
@MuraadNofal Ok I will give that a try as wellSwiger
I can't believe nobody's tried to explain what HTTP error 405 actually means. It means you're using the wrong request verb (GET, POST, PUT, etc.). The fix to the first error could be as simple as replacing GetAsync with PostAsync. As it stands, the second error appears to be a knock-on of the first having failed. I haven't looked into this in detail but you may need to pull some data out of the response to the first request and pass that to the second.Acea
O
1

Checkout the details of obtaining oauth2 access token on the Google API OAuth2 Flow (Client side) page - https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps#Obtaining_Access_Tokens

You will notice that the resource to GET an access token is o/oauth2/auth and not o/oauth2/token (which seems to be what you are using from your code snippet) - and that should be the reason for the 405 (Method Not Allowed) response from the server.

In the google api version that you are using, could you check if o/oauth2/token is the right resource to GET the access token in exchange for the client id and client secret using the oauth2 auth_code grant type?

As a couple others have pointed out a 405 response suggests that the method is not allowed for the requested resource. And it seems to me that the correct resource to GET in API version 3 is o/oauth2/auth.

Looking at your code snippet, it seems to me that you are sending a GET request to the o/oauth2/token resource sending over your "AccessKey", your client_id and your client_secret and oauth2 grant_type=auth_code and expecting to get back a access token and a refresh token; and you expect to use the access token thus received to make the next GET request.

However, as stated earlier, it does not appear that o/oauth2/token is the right resource to GET an access token in Youtube API v3 (assuming you are trying to make this work on v3)

Also, check out https://developers.google.com/youtube/v3/getting-started <-- YouTube API (v3) Getting Started Guide and make sure that you have followed the steps there in; especially, ensure that "In the list of APIs, make sure the status is ON for the YouTube Data API v3." as suggested in 3b. on the getting started page.

Let me know if you are not using (/trying to use) v3 of the API.

Orthognathous answered 11/10, 2015 at 10:1 Comment(1)
worked for me - I'm using the v3 version of the api - accounts.google.com/o/oauth2/token is the which I should be used. I will take a closer look at the api guides.Swiger
N
2

If you cannot use 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

Nephrosis answered 11/10, 2015 at 10:28 Comment(0)
O
1

Checkout the details of obtaining oauth2 access token on the Google API OAuth2 Flow (Client side) page - https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps#Obtaining_Access_Tokens

You will notice that the resource to GET an access token is o/oauth2/auth and not o/oauth2/token (which seems to be what you are using from your code snippet) - and that should be the reason for the 405 (Method Not Allowed) response from the server.

In the google api version that you are using, could you check if o/oauth2/token is the right resource to GET the access token in exchange for the client id and client secret using the oauth2 auth_code grant type?

As a couple others have pointed out a 405 response suggests that the method is not allowed for the requested resource. And it seems to me that the correct resource to GET in API version 3 is o/oauth2/auth.

Looking at your code snippet, it seems to me that you are sending a GET request to the o/oauth2/token resource sending over your "AccessKey", your client_id and your client_secret and oauth2 grant_type=auth_code and expecting to get back a access token and a refresh token; and you expect to use the access token thus received to make the next GET request.

However, as stated earlier, it does not appear that o/oauth2/token is the right resource to GET an access token in Youtube API v3 (assuming you are trying to make this work on v3)

Also, check out https://developers.google.com/youtube/v3/getting-started <-- YouTube API (v3) Getting Started Guide and make sure that you have followed the steps there in; especially, ensure that "In the list of APIs, make sure the status is ON for the YouTube Data API v3." as suggested in 3b. on the getting started page.

Let me know if you are not using (/trying to use) v3 of the API.

Orthognathous answered 11/10, 2015 at 10:1 Comment(1)
worked for me - I'm using the v3 version of the api - accounts.google.com/o/oauth2/token is the which I should be used. I will take a closer look at the api guides.Swiger
T
0

If you could supply a value for localSettings.Values["AccessKey"] that causes the error, it would help. My guess here is that this variable contains characters that should be encoded. Oon you comment your said localSettings.Values["AccessKey"] might be X/XXXXX. If the resulting url is something like /token?code=X/XXXXX"..., you could try /token?code=X%2FXXXXX

Tartrate answered 8/10, 2015 at 19:47 Comment(3)
I Will give that a trySwiger
localSettings.Values["AccessKey"].ToString().Replace("/","%2F") didn't help me out sadlySwiger
Sorry man, I out of ideas. Maybe Do it like this: "..." + HttpUtility.UrlEncode(localSettings.Values["AccessKey"]) + "..." Give it a tryTartrate
H
0

Your first request should be a HTTP POST call. Presently you are using GET. GET it seems is causing the error 405, which indicates that the GET method is not allowed.

Heterophyllous answered 11/10, 2015 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.