Matthias Herrmann
Matthias Herrmann

Reputation: 2790

Http Request Status Code 405 ReasonPhrase: '' Youtube APi

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
}

Upvotes: 6

Views: 1472

Answers (4)

IgorL
IgorL

Reputation: 1169

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

Upvotes: 2

g7p
g7p

Reputation: 126

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.

Upvotes: 1

Santanu Dey
Santanu Dey

Reputation: 2978

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.

Upvotes: 0

victor
victor

Reputation: 1552

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

Upvotes: 0

Related Questions