Skip to content
Advertisement

How fetch access token with auth code for google api using php?

I am working on google docs api and I want to send request and get the auth code which in exchange will give the access token. Below code is working fine but the problem is I have to copy the auth url and paste it to the browser and then from browser url, I am copying the auth code and pasting it to the terminal which in return a token.json file is being created it my directory. But the thing is I want to implement the same thing in my project and I can’t do it like this copying url from one place to another.I want it all dynamically.

can anybody help in this how we can modify the below code for sending auth url request and in return i get auth code from which i can fetch the access token without copying and pasting it to the terminal for processing.

function getClient()
{
    $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"
                        ]);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');

    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory('token.json');
    
    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();        
        $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 %sn", $credentialsPath);
    }

    $client->setAccessToken($accessToken);

    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {

        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
       
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
    
    return $client;
}

Advertisement

Answer

The PHP client library is not designed to open the web browser for you with console applications or installed applications. You need to show the user the Oauth2 browser link that they can then open in a browser and then paste back into the code.

The library does not support the functionality to open the browser window for you in a console application.

       // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:n%sn", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));

        // Exchange authorization code for an access token.
        $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
        $client->setAccessToken($accessToken);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement