Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API - request for token from Oauth2 returns “invalid_request” with HTTP code 400

I know same question has been asked many times but even after following each and every post as well trying various suggestions, I am still facing invalid_request error.

I am able to get code successfully from Google API and when I am trying to access accessToken , I am getting this error and HTTP code being sent from Google API is 400.

I have already tried and compared my data with oauthplayground and seems everything is same

Following data is being sent from my Application to Google API

https://accounts.google.com/o/oauth2/auth?
scope=openid+profile+email
&response_type=code
&redirect_uri=http%3A%2F%2Fsavealifetoday.org%2Fblood_donors%2FoAuthCallBackAction%3Fservice_provider_name%3D
&approval_prompt=force
&client_id=193214746340-ea0nq5alpst82n2fno13s9j21pn4f4pf.apps.googleusercontent.com

Access Token URL

https://accounts.google.com/o/oauth2/token?
scope=openid+profile+email
&client_secret=***********
&grant_type=authorization_code
&redirect_uri=http%3A%2F%2Fsavealifetoday.org%2Fblood_donors%2FoAuthCallBackAction%3Fservice_provider_name%3D
&code=4%2F8veoAnihzkao58oWvZaRShn5nhYg.0to7Or-lHPwRXE-sT2ZLcbTblHXFhgI
&client_id=193214746340-ea0nq5alpst82n2fno13s9j21pn4f4pf.apps.googleusercontent.com

But When Application is trying to fetch data over the network, I am getting exception

SEVERE: Error while fetching access token for Google..
OAuthProblemException{error='invalid_request', description='null', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}

I am using URLConnectionClient to establish network connection

like image 971
Umesh Awasthi Avatar asked Aug 23 '26 12:08

Umesh Awasthi


2 Answers

Go through the OAuth flow using the OAuth playground https://developers.google.com/oauthplayground/.

Then compare what is being sent with your own.

Note that it needs to be an HTTP POST. The code you posted looks like a GET.

Also, your redirect URL looks a little odd. Is that exactly the same URL that you registered in the API console? This is isn't your current error, but will probably become your next one ;-)

like image 83
pinoyyid Avatar answered Aug 25 '26 03:08

pinoyyid


It seems that you forgot to send 'state' parameter here (it is mandatory!):

https://accounts.google.com/o/oauth2/auth?
scope=openid+profile+email
&response_type=code
&redirect_uri=http%3A%2F%2Fsavealifetoday.org%2Fblood_donors%2FoAuthCallBackAction%3Fservice_provider_name%3D
&approval_prompt=force
&client_id=193214746340-ea0nq5alpst82n2fno13s9j21pn4f4pf.apps.googleusercontent.com

In Java code you can generate it in a way like this:

String state = new BigInteger(130, new SecureRandom()).toString(32);

So you request should look like this:

https://accounts.google.com/o/oauth2/auth?
scope=openid+profile+email
&response_type=code
&redirect_uri=http%3A%2F%2Fsavealifetoday.org%2Fblood_donors%2FoAuthCallBackAction%3Fservice_provider_name%3D
&approval_prompt=force
&client_id=193214746340-ea0nq5alpst82n2fno13s9j21pn4f4pf.apps.googleusercontent.com
&state= {insert_here_generated_state}

And also I don't know if it is necessary to send a 'scope' parameter in "Access Token URL".

like image 42
heroin Avatar answered Aug 25 '26 04:08

heroin