Search code examples
google-apigoogle-drive-apigoogle-oauthgoogle-api-php-clientgoogle-docs-api

How I can create a token.json file programatically or just increase the expiration time of my token in Google API?


I have made one feature in my web application using Google Docs API and Google Drive API. In that case I am using following below code with the help of the following document Google Docs php quick start ,

I am using PHP :-

<?php

    require __DIR__ . '/../vendor/autoload.php';
    
    if (!defined('STDIN')) {
        define('STDIN', fopen('php://stdin', 'r'));
    }

    $client = new Google_Client();
    $client->setApplicationName('Google Docs API PHP Quickstart');
    $client->setScopes([
        "https://www.googleapis.com/auth/documents",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/drive",
        Google_Service_Drive::DRIVE_READONLY,
        ]);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');
    $credentialsPath = expandHomeDirectory('token.json');
    
    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } else {
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));
        $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

        if (!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }

        file_put_contents($credentialsPath, json_encode($accessToken));
    }

    $client->setAccessToken($accessToken);

    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path)
{
    $homeDirectory = getenv('HOME');
    if (empty($homeDirectory)) {
        $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
    }
    return str_replace('~', realpath($homeDirectory), $path);
}

In this scenario what I have to do is the following steps-

  1. Run php quickstart.php in my terminal, after that I get one link and after I open that link with specific google account by allowing the scopes I have added , I get an auth code in the url.
  2. I have to copy and paste that Auth code in the terminal.
  3. After the second step, A token.json file is created with access token and refresh token with its expiration time.

But what is happening like My token gets expire after 7 days and again I have to do it manually with the above mentioned steps. So in that scenario my web app doesn't work until I do the above mentioned steps to create token.json file again.

So, is there any way like My token.json file gets created programmatically after its expiration period of time or Is there any way to just increase the expiration time.

It would be a great help, I would love to appreciate as I am stuck at this point from months.

FYI - my app in google console developer site is in testing mode.So do I need to publish it for overcoming my stuck problem.


Solution

  • Your refresh token is expiring after seven days because your application is still in the testing phase. What you need to do is put your application into production by clicking the button on google cloud console.

    Just set it to production and your refresh token will stop expiring.