googleworkspace / php-samples

PHP samples for Google Workspace APIs
Apache License 2.0
288 stars 348 forks source link

Use of undefined constant STDIN - assumed 'STDIN' #33

Closed g4br13lg4l4n closed 5 years ago

g4br13lg4l4n commented 5 years ago

(Please search existing issues before creating a new one.)

Sample:

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

Steps to Reproduce the Problem

if ($client->isAccessTokenExpired()) {
    // Refresh the token if possible, else fetch a new one.
    if ($client->getRefreshToken()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    } 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);
        $client->setAccessToken($accessToken);

        // Check to see if there was an error.
        if (array_key_exists('error', $accessToken)) {
            throw new Exception(join(', ', $accessToken));
        }
    }
    // Save the token to a file.
    if (!file_exists(dirname($tokenPath))) {
        mkdir(dirname($tokenPath), 0700, true);
    }
    file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}

Specifications

erickoledadevrel commented 5 years ago

This code is designed to be run from the PHP command line (CLI) environment, where STDIN should be defined. Are you running it from a web server instead?

rajeevbbqq commented 5 years ago

@erickoledadevrel When I tried from a server, I got Use of undefined constant STDIN - assumed 'STDIN'

erickoledadevrel commented 5 years ago

That's correct, it's only present when running PHP on the command line, as the sample was intended.