google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Automatically fetch and set new access tokens using the refresh Token

Open bshaffer opened this issue 5 years ago • 11 comments

After viewing the Gsuite Quickstart, they are implementing their own check for fetching a new access token via a refresh token. We have logic to do this already, so either it isn't working as expected, or we need to update these quickstart samples with the correct way.

bshaffer avatar Jan 16 '19 23:01 bshaffer

Hey @bshaffer. I'm getting a 404 on your link. Could you double that for me? Thanks!

danielgsims avatar Jan 20 '19 04:01 danielgsims

@danielgsims Is it still 404ing for you?

tmatsuo avatar Apr 16 '19 19:04 tmatsuo

The Docs API may have been private at the time. It doesn't 404 anymore. Not sure about auto-refreshing.

See this sample for better G Suite quickstart: https://developers.google.com/slides/quickstart/php

grant avatar Jun 14 '19 19:06 grant

@bshaffer

I guess you mean that, PHP library should automatically refresh the access token when it's expired. So only thing that users need to do is to set the access token array with $client->setAccessToken().

I also think it's true. For example, the quickstart just works when commenting out the line calling fetchAccessTokenWithRefreshToken.

tmatsuo avatar Dec 03 '19 19:12 tmatsuo

@bshaffer What's the proper way to do all this? For example, if you rewrite the sheets quickstart, what would it be?

tmatsuo avatar Dec 03 '19 19:12 tmatsuo

The refreshing should happen transparently, so the quickstart wouldn't look different WRT that aspect. However, the quickstarts look pretty bad right now so I would like the quickstart to look more something like this:

use Google\Client;
use Google\Service\Docs;
use Google\Cache\Filesystem;

/**
 * Create an authorized API client.
 * Be sure you've set up your OAuth2 consent screen at
 * https://console.cloud.google.com/apis/credentials/consent
 */
$client = new Client([
    'authConfig' => $clientCredentialsPath,
    'scopes' => Docs::DOCUMENTS_READONLY,
    'accessType' => 'offline',
    'cache' => new Filesystem(__DIR__) // You can use any PSR-6 compatible cache
]);

// Fetch our Access Token if it does not exist or is expired.
if ($client->isAccessTokenExpired()) {
    // Request the user for authorization via the CLI.
    // For setting up a request through the browser
    // @see SOME_AUTH_DOCUMENTATION_URL
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter the verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $client->fetchAccessTokenWithAuthCode($authCode);
}

$service = new Docs($client);
$doc = $service->documents->get($documentId);
printf($doc->getTitle());

bshaffer avatar Dec 03 '19 21:12 bshaffer

Well, then I think there is nothing we need to do within this repo? If that's the case, I suggest that we close this issue, and open a new issue against G suite docs components. WDYT @bshaffer ?

tmatsuo avatar Dec 03 '19 21:12 tmatsuo

Oh, I'm little bit confused. The code sample in https://github.com/googleapis/google-api-php-client/issues/1580#issuecomment-561357447 seems like requiring a future version of our library?

Anyways, it doesn't work with our current library. Correct me if I'm wrong.

Well, if the current G suite quickstarts are fine with the current PHP library, there's nothing wrong with those documentation.

tmatsuo avatar Dec 03 '19 21:12 tmatsuo

Yes, sorry, that sample was to suggest improvements to this library regarding making the library more usable. It's tangentially related to this issue only.

I believe there's still work we can do in this repo, to resolve this issue, so I think we should leave it open. WDTY?

bshaffer avatar Dec 03 '19 22:12 bshaffer

@bshaffer sounds good.

tmatsuo avatar Dec 03 '19 22:12 tmatsuo

I think various documentation implies that this is already done: https://developers.google.com/identity/protocols/oauth2/web-server

"The client object will refresh the access token as needed." under the PHP and Python examples.

ryanawhelan avatar Jun 07 '22 05:06 ryanawhelan