D
D
direx2016-08-24 01:26:57
Google
direx, 2016-08-24 01:26:57

Infinite token for Google Api Service?

Guys, I'm using google/apiclient in a project. To work with this api, you need to log in and get a token with which the library will work. It looks like this:

protected function getClient()
    {
        $client = new Google_Client();
        $client->setApplicationName(APPLICATION_NAME);
        $client->setScopes(SCOPES);
        $client->setAuthConfig(CLIENT_SECRET_PATH);
        $client->setAccessType('offline');

        // Load previously authorized credentials from a file.
        $credentialsPath = CREDENTIALS_PATH;
        if (file_exists($credentialsPath)) {
            $accessToken = json_decode(file_get_contents($credentialsPath), true);
        } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

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

            // Store the credentials to disk.
            if (!file_exists(dirname($credentialsPath))) {
                mkdir(dirname($credentialsPath), 0700, true);
            }
            file_put_contents($credentialsPath, json_encode($accessToken));
            printf("Credentials saved to %s\n", $credentialsPath);
        }
        $client->setAccessToken($accessToken);


        // Refresh the token if it's expired.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));

        }
        return $client;
    }

After that, the authorization data will be written to $credentialsPath . But! After expires_in expires , the client will refresh its token by sending a refresh_token to the server . After updating the token, everything works again until you need to update it again.
I also noticed that after:
// Refresh the token if it's expired.
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
        }

Refresh_token is not written to the file . That is, when you need to update, this token is not in the file.
Who faced similar?
File on first login:
{"access_token":"ya29.Ci9IA7H-*******-*********","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/**********-*******","created":1471990656}

The file after the first refresh of the token:
{"access_token":"ya29.Ci9IA7H-*******-*********","token_type":"Bearer","expires_in":3600,"created":1471990987}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Malyshev, 2016-09-01
@PaulMaly

Yes, I did. For Google, this is normal. It sends refresh_token only once. If the refresh_token has already been issued to you, then do not wait for it anymore. Just don't overwrite the previous one and use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question