google oauth refresh token code example

Example 1: google oauth2 does token change when refresh

The Google Auth server issued Refresh tokens
never expire — that's the whole point of the
refresh tokens. The refresh token will
expire (or I should say become unauthorized) when the user
revokes access to your application.

Example 2: google apis does refresh token chage

The Google Auth server issued Refresh tokens never expire —
that's the whole point of the refresh tokens. The refresh token
will expire (or I should say become unauthorized) when the user
revokes access to your application.

Refer this doc it clearly states the function of refresh tokens.

Example 3: get refresh token google api

$token = '{"access_token":"' . $token_data->access_token . '","expires_in":' . $token_data->expires_in . ',"scope":"https:\/\/www.googleapis.com\/auth\/business.manage","token_type":"' . $token_data->token_type . '","created":' . $token_data->created . '}';
$client = new \Google_Client();
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/../../../../public/assets/client_secrets.json');
$client->addScope(\Google_Service_MyBusiness::BUSINESS);
// Set the access token on the client.
try {
  if ( $client->isAccessTokenExpired() )
  {
   $this->refreshAccessToken($token_data, $client, 'gmb_user', $user_id, $brand->id);
  }
  else{
    $client->setAccessToken($token);
  }
}
catch (\Exception $exception){

}

private function refreshAccessToken ($token_data, $client, $table, $user_id, $brand_id) {
  Log::info('Token Expired 1:'.json_encode($token_data->refresh_token));
  Log::info('Old Token:'.json_encode($token_data->access_token));
  $access_token = $client->fetchAccessTokenWithRefreshToken($token_data->refresh_token);
  Log::info('Renewed Token:'.json_encode($access_token));

  $client->setAccessToken($access_token);

  $result = DB::table($table)
    ->where(['owner_id' => $user_id, 'brand_id' => $brand_id])
    ->update(['access_token' => $client->getAccessToken()["access_token"],
              'expires_in' => $client->getAccessToken()["expires_in"],
              'token_type' => $client->getAccessToken()["token_type"],
              'created' => $client->getAccessToken()["created"]
             ]);
}

Example 4: documentation for https://www.googleapis.com/oauth2/v4/token

>>> # Credentials you get from registering a new application
>>> client_id = '<the id you get from google>.apps.googleusercontent.com'
>>> client_secret = '<the secret you get from google>'
>>> redirect_uri = 'https://your.registered/callback'

>>> # OAuth endpoints given in the Google API documentation
>>> authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
>>> token_url = "https://www.googleapis.com/oauth2/v4/token"
>>> scope = [
...     "https://www.googleapis.com/auth/userinfo.email",
...     "https://www.googleapis.com/auth/userinfo.profile"
... ]

>>> from requests_oauthlib import OAuth2Session
>>> google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)

>>> # Redirect user to Google for authorization
>>> authorization_url, state = google.authorization_url(authorization_base_url,
...     # offline for refresh token
...     # force to always make user click authorize
...     access_type="offline", prompt="select_account")
>>> print 'Please go here and authorize,', authorization_url

>>> # Get the authorization verifier code from the callback url
>>> redirect_response = raw_input('Paste the full redirect URL here:')

>>> # Fetch the access token
>>> google.fetch_token(token_url, client_secret=client_secret,
...         authorization_response=redirect_response)

>>> # Fetch a protected resource, i.e. user profile
>>> r = google.get('https://www.googleapis.com/oauth2/v1/userinfo')
>>> print r.content

Tags:

Go Example