googleapis / google-api-php-client

A PHP client library for accessing Google APIs
http://googleapis.github.io/google-api-php-client/
Apache License 2.0
9.32k stars 3.52k forks source link

refresh_token not return #2519

Closed lRafaelOliveira closed 10 months ago

lRafaelOliveira commented 10 months ago

eu usava o a lib para autenticar com oauth2, funcionava bem até ontem... porem hoje, quando faço a autenticacao: $client->fetchAccessTokenWithAuthCode($dados->code)

recebo um array com as info: Array ( [access_token] => ya2 [expires_in] => 3599 [scope] => www.... [token_type] => Bearer [created] => 1700232993 )

o fato é que anteriormente me retornava nesse array, o refesh_token, que eu utilizava para poder atualizar o meu token de acesso.

alguem sabe o que aconteceu? ou como obter o token de acesso ?

bshaffer commented 10 months ago

Hello @lRafaelOliveira

The refresh token is only returned with the auth code when the "access type" is set to "online". You will need to do something like this to make sure you receive a refresh token:

$client->setAccessType('offline');

This needs to happen before you receive the authorization code, however, so that access_type=offline appears in the query string of the request to get the authorization code. For instance:

$client = new Google\Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->setAccessType('offline');        // offline access
$authUrl = $client->createAuthUrl();

and then in your HTML:

<a href="<?= $authUrl ?>" connect me! </a>

See the simple file upload example for a full example.