How to get new acess token from refres token for google api in postman

Viewed 390

I am trying to get new access token from my refresh token for google drive api. In my google playground it works but when I want to create the same request in postman or my code it doesn't work and I always get error "Invalid grand type". I don't know to find what is problem.

google developers playground enter image description here

postman headers and body enter image description here enter image description here

1 Answers

You need to understand that there is three steps to the Oauth2 dance.

step one is requesting access of the user and getting the authorization code.

HTTP GET https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code

Note &response_type=code tells the server you want an authorization code returned.

  1. step two is to exchange that code for an access token and refresh token.

POST https://accounts.google.com/o/oauth2/token code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

Note the part where it says &grant_type=authorization_code. this tells the server you are giving them an authorization code.

3 the final step is refreshing your access token.

POST https://accounts.google.com/o/oauth2/token client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token

Note &grant_type=refresh_token you are telling the server you are sending them a refresh token.

You appear to be sending a refresh token with the wrong grant type.

I have a video on how to set up postman to use google oauth2

Note: Due to a recent change with Making Google OAuth interactions safer by using more secure OAuth flows redirect uri of urn:ietf:wg:oauth:2.0:oob is going to stop working soon.

Related