使用PHP API将文件上传到Google云端硬盘

时间:2020-07-16 05:59:40

标签: php google-api google-drive-api google-api-php-client

我目前正在尝试使用php API将测试文件上传到Google驱动器。我收到以下错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission: Request had insufficient authentication scopes."
   }
  ],
  "code": 403,
  "message": "Insufficient Permission: Request had insufficient authentication scopes."
 }
}

这是我创建客户端的方式:

$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

$service = new Google_Service_Drive($this->client);

这是我用来上传文件的代码:

$file = new Google_Service_Drive_DriveFile();
        $file->setName(uniqid().'.jpg');
        $file->setDescription('A test document');
        $file->setMimeType('image/jpeg');

        $data = file_get_contents('test.jpg');

        $createdFile = $this->service->files->create($file, array(
            'data' => $data,
            'mimeType' => 'image/jpeg',
            'uploadType' => 'multipart'
        ));

        print_r($createdFile);

感谢您的帮助。谢谢!

0 个答案:

没有答案