4

I am trying to get a token from the Google OAuth api. I have already successfully been able to get the temporary auth code.

However, my requests are returning a 404 error, with the normal Google "That's an error" 404 page. Here is my Python code:

data = { "code":auth_code, "client_id":client_id, "client_secret":client_secret, "redirect_uri":redirect_uri, "grant_type":"authorization_code" } headers = {"Content-Type":"application/x-www-form-urlencoded"} r = requests.post("https://googleapis.com/oauth/v4/token",data=data,headers=headers) 

I get the same error no matter if I url-encode my parameters or not (I think the requests library does it anyway).

Here are the data I am sending in more detail (censored of course)

'client_id':'2-------------------------------------------0.apps.googleusercontent.com', 'client_secret': '5----------------------p', 'code': '4/A-------------------------- ... ------------------------------------fGE#', 'grant_type': 'authorization_code', 'redirect_uri': 'https://localhost' 

I understand the question here is very similar to mine but all the solutions provided either don't work (url encoding) or don't apply (everything else).

I am using this official documentation for reference.

This is probably very obvious, like most of the questions I ask here.

Edit - I tried

data = "code="+auth_code+"&client_id="+client_id+"&client_secret="+client_secret+"&redirect_uri="+redirect_uri+"&grant_type=authorization_code" 

...which returned a 400. With or without url-encoding.

4 Answers 4

11

For me the issue was that I was sending a GET. You have to send a POST.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I wonder why wouldn't they return something like "405 Method not allowed" - it would be way more helpful ...
2

Figured it out.

I didn't add my redirect uri to the list of authorized ones as the option doesn't appear if you set your app type to "Other". I set it to "Web Application" (even though it isn't) and added my redirect uri and that fixed it.

Comments

1

Your code snippet lists "https://googleapis.com/oauth/v4/token".

The token endpoint is "https://googleapis.com/oauth2/v4/token".

1 Comment

Ok, scrap that. "r = requests.post("googleapis.com/oauth2/v4/token",data=data,headers=headers)" is 404ing as well.
0

In my edge case, I was requesting the token pass

 val tokenRequest = Request.Builder() .method( "POST", "".toRequestBody("application/x-www-form-urlencoded".toMediaType()) ) 

The snippet above didn't work until I changed "POST" to "post".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.