jeudi 1 juillet 2021

Use of undefined constant STDIN - assumed 'STDIN' when integrating google calendar api

I am integrating google calendar api and I am following their quickstart quide. But I am encountering one error which says

Use of undefined constant STDIN - assumed 'STDIN' (this will throw an Error in a future version of PHP)

Below is the code from google's quickstart guide

public function getClient()
    {
        $client = new \Google_Client();
        $client->setApplicationName('Dragon');
        $client->setScopes(\Google_Service_Calendar::CALENDAR_READONLY);
        $client->setAuthConfig(storage_path('app/google-calendar/credentials.json'));
        $client->setAccessType('offline');
        $client->setPrompt('select_account consent');

        $tokenPath = 'token.json';
        if (file_exists($tokenPath)) {
            $accessToken = json_decode(file_get_contents($tokenPath), true);
            $client->setAccessToken($accessToken);
        }
    
        // // If there is no previous token or it's expired.
        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($name);
                $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()));
        }
        return $client;
    }

I google this error and there seems to be some solutions like below but that also didn't worked for me.

I place the below code in my artisan file of laravel directory or even tried to putting it right above where I call this constant but that also didn't worked for me.

define('STDIN',fopen("php://stdin","r"));

As you can see above code seems to be the solution all over the internet but it didn't worked.

Can anyone please tell me what I am doing wrong and missing.

Thank You.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire