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.34k stars 3.52k forks source link

Automatically fetch and set new access tokens using the refresh Token #1580

Open bshaffer opened 5 years ago

bshaffer commented 5 years ago

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.

danielgsims commented 5 years ago

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

tmatsuo commented 5 years ago

@danielgsims Is it still 404ing for you?

grant commented 5 years ago

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

tmatsuo commented 4 years ago

@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 commented 4 years ago

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

bshaffer commented 4 years ago

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());
tmatsuo commented 4 years ago

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 commented 4 years ago

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.

bshaffer commented 4 years ago

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?

tmatsuo commented 4 years ago

@bshaffer sounds good.

ryanawhelan commented 2 years ago

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.