3

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

3
  • What happens if you take out the grant_type? Commented Jan 6, 2014 at 0:06
  • @lumpynose: Same issue...still getting this error OAuthProblemException{error='invalid_request', description='null', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}} Commented Jan 6, 2014 at 1:48
  • Do you receive response after first request? And does error happen only after the second request? Commented May 19, 2014 at 8:24

2 Answers 2

3

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

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

2 Comments

Thanks for the update, Yes I have same redirect URL, since few other OAuth provider do not allow localhost ;). I have already seen it on playground and seems like everything is fine.I am doing HTTP POST
I getting the error till now. I have matched with oauthplayground. Its same. What to do?
1

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.