Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh oauth2 token google api and HWIOAuthBundle

How i can refresh token ? I use Google api with this token - it work but can't find how to refresh it, in this example we dont save expired time. I require

`access_type:     offline `

then

$client = new Google_Client();
        //$client->setClientId($GoogleClientId);
        $client->setApplicationName($GoogleAppName);
        $client->setClientId($this->user->getGoogleId());
        $client->setAccessType('offline');

if token is valid i can work but when is expired i try

$token = [
            'access_token' => $this->user->getGoogleAccessToken(),
            'expires_in'   => (new \DateTime())->modify('-1 year')->getTimestamp(),
        ];

i put this any date because in this example we don't save expired time

https://gist.github.com/danvbe/4476697

    $client->setAccessToken($token);

    if($client->isAccessTokenExpired()){

        $refreshedToken = $client->refreshToken($client->getAccessToken());

here i have error

array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Could not determine client ID from request."
]

There is HwiAuthBundle method to refresh token ? Why this not work with Google_Client refresh ?

like image 788
Developer Avatar asked Nov 10 '16 08:11

Developer


1 Answers

In oauth2.0 to refresh an expired access token you need to send to the endpoint :

  • a grant type equals to 'refresh_token'
  • a valid refreshToken
  • your clientId
  • and your clientSecret

You can't send an expired accessToken to get a new refreshed accessToken.

public function refreshAccessToken($refreshToken, array $extraParameters = array())
{
    $parameters = array_merge(array(
        'refresh_token' => $refreshToken,
        'grant_type' => 'refresh_token',
        'client_id' => $this->options['client_id'],
        'client_secret' => $this->options['client_secret'],
    ), $extraParameters);
    $response = $this->doGetTokenRequest($this->options['access_token_url'], $parameters);
    $response = $this->getResponseContent($response);
    $this->validateResponseContent($response);
    return $response;
}

function refreshAccessToken($refreshToken, ...

and not $accessToken

I think you need to call after construct your client with your credentials

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->refreshToken($client->getRefreshToken());

https://developers.google.com/api-client-library/php/auth/web-app#creatingcred

Are you sure of your $client->setClientId($this->user->getGoogleId()); ? What is getGoogleId() ? I think you need also to create a oauth client id : https://developers.google.com/identity/sign-in/web/devconsole-project

In oauth client_id is not the user id but the app id

like image 108
François LEPORCQ Avatar answered Oct 17 '22 20:10

François LEPORCQ