dqfr90979 2017-02-08 15:17
浏览 75

通过Google Drive PHP库API V3将文件从Google驱动器复制到我们的服务器

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:
%s
", $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
", $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()));
        }
  • 写回答

1条回答 默认 最新

  • douyinjiao9351 2017-02-09 08:57
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么