1

I'm trying to integrate Google Drive within our website. What we would like to do is to copy a file from Drive to our server, you know, kind of : Send to My Files. When i try to integrate i am facing the following error

enter image description here

My tried code:

require_once(ROOT . DS . 'vendor' . DS . 'google-api-php-client' . DS . 'vendor' . DS . 'autoload.php');

        define('APPLICATION_NAME', 'Drive API PHP Quickstart');
        define('CREDENTIALS_PATH', ROOT . DS . 'vendor' . DS. 'google-api-php-client' . DS .'vendor' . DS . 'drive-php-quickstart.json');
        define('CLIENT_SECRET_PATH', ROOT . DS . 'vendor' . DS. 'google-api-php-client' . DS . 'vendor' . DS . 'client_secret.json');
        define('SCOPES', implode(' ', array(\Google_Service_Drive::DRIVE_METADATA_READONLY)));


        $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 = $this->expandHomeDirectory(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->isAccessTokenExpired()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
        }
Dinesh
  • 197
  • 1
  • 12

1 Answers1

0

You error Notice: Use of undefined constant STDIN means it was looking for constant called STDIN. When it doesn't find such a constant, PHP interprets it as a string. Obviously, this can easily break if you do defined such a constant later. It stated here that you should quote your array key to avoid such error. The error message is due to the unfortunate fact that PHP will implicitly declare an unknown token as a constant string of the same name. You may also check on this link: Copy a file from Google Drive to my own server

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59