ssuperczynski
ssuperczynski

Reputation: 3416

Google Api, how can I refresh user token when I have acces token and previous refresh token

Is it possible to refresh Google Api token when I have previous refresh token and access token using pure php, without Google Api library? I am storing in database many users refresh and access tokens.

I am using url from this address:

https://developers.google.com/identity/protocols/OAuth2WebServer?hl=en#creatingcred

and using this address:

https://accounts.google.com/o/oauth2/v2/auth?
 scope=email%20profile&
 state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&
 redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Fcode&,
 response_type=code&
 client_id=812741506391.apps.googleusercontent.com

All the time I am being redirected somewhere, and not getting new access token. But, why do I have to put redirect_uri?

Additionally under my Google API Manager in Credential section I have some Client ID and Client secret but Client ID 1076711322609-s9mrrfp3t8gto2qkrj4ud3jjhr3rr0ph.apps.googleusercontent.com is different than this one client_id=812741506391.apps.googleusercontent.com Why?

So the question is, how can I get access to users Google API endpoint using their access tokens, and refresh those tokens every hour using PHP (REST calls) without any prompts, logins and passwords. Is it even possible?

enter image description here

Upvotes: 3

Views: 343

Answers (2)

ssuperczynski
ssuperczynski

Reputation: 3416

So after many attempts I found working solution.

===EDIT==== @DaImTo answer is much better

POST

https://developers.google.com/oauthplayground/refreshAccessToken

Headers:

Content-Type: application/json

Body:

{
    "refresh_token": "YOUR_PREVIOUS_REFRESH_TOKEN"
}

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116908

You can get a new access_token you take the refresh_token that you have stored in your database and HTTP Post it to the following url. Note: grant_type=refresh_token

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

The response

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

Upvotes: 0

Related Questions