6

Im trying to use google oauth using the below link but get a 405 error,

Can you please let me know if the parameters are correct?

client_id = changed to a diff value
response_type = code
scope= openid%20email
redirecturl = given the value based on what I registered in console.developers.com
login_hint = my gmail id..

https://accounts.google.com/o/oauth2/token?
 client_id=690178314820-85fvo4eq56se4mppdaf0pt6tnnjo552&
 response_type=code&
 scope=openid%20email&
 redirect_uri=http://test.webfactional.com&
 state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&
 [email protected]

I made the above get requests in the browser..

3 Answers 3

4

There are a few steps to getting access to Google its easer for me to show you the full flow. My guess is you are stuck on step two because your not sending it as a post.

Step 1: Ask for access

https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri={From console}&scope=openid%20email&response_type=code

This just displays the window asking them to approve you. Once the user has approved access you get a one time Authentication Code.

Step 2: Exchange Authentication Code for AccessToken and RefreshToken. Note this needs to be sent as a HTTP POST not a HTTP Get.

https://accounts.google.com/o/oauth2/token
code={Authentication Code from step 1}&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=={From console}&grant_type=authorization_code

you should get a JSon string back looking something like this.

{
"access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
}

Now you can take that Access_token and use it to make your requests. But access tokens are only good for 1 hour and then they expire before that time you need to use the Refresh_token to get a new access token. Also if you are going to want to access your users data again you should save the refresh_token some place that will enable you to always access there data.

Step 3: Use Refreshtoken

https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token={RefreshToken from step 2}&grant_type=refresh_token

This time you will only get the Access token back, because your refreshtoken is good until the user removes authentication or you haven't used it for 6 months.

{
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
"token_type" : "Bearer",
"expires_in" : 3600
}

You can find more detailed information on this here Google 3 Legged oauth2 flow

3
  • The first step worked fine..In the second the connection went fine but I got the following response....access_token, token_type, expires_in and id_token..I dont see anything as refresh token..Is id_token can be considered as refresh token? Commented Jun 8, 2014 at 3:20
  • 1
    I set 'access_type':'offline' as specified in the google doc to get refresh_token but I get the following error " "error_description" : "Parameter not allowed for this message type: access_type"\n}'" Commented Jun 8, 2014 at 3:28
  • This still ask me to Accept, there is option without this?
    – kicaj
    Commented Nov 18, 2014 at 19:33
0

It seems you are using wrong api, you should use https://accounts.google.com/o/oauth2/auth instead of https://accounts.google.com/o/oauth2/token.

The reason you get error 405 is https://accounts.google.com/o/oauth2/token can only be called by POST, and it is to get token. You need to get authorization code first and then exchange it for a token.

3
  • Thanks....accounts.google.com/o/oauth2/token will get me the token...How will I get the authorization...What url link that needs to be used? Commented Jun 4, 2014 at 2:18
  • use https://accounts.google.com/o/oauth2/auth?client_id=YOUR_CLIENTID&response_type=code&scope=YOUR_SCOPE&redirect_uri=YOUR_REDIRECTURI You can refer this
    – Owen Cao
    Commented Jun 4, 2014 at 2:25
  • I used the above link changing token by auth but its failing Commented Jun 4, 2014 at 2:29
0

Please pay attention for this /oauth2/v3/token and /oauth2/token

I do follow guide of google at this link

It show me following

  1. obtain Authentication Code by /o/oauth2/auth => it work, the response as example in the guide

  2. obtain access token by /oauth2/v3/token => it is error, the status code 405 is responsed

The correct must be /oauth2/token

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.