php中没有足够的权限google drive api oauth2

时间:2016-12-01 11:11:39

标签: php google-drive-api

我正在尝试通过PHP上传谷歌驱动器上的文件。我在开发者控制台中创建了一个项目,启用了Drive API并添加了OAuth 2.0客户端ID。 这是我的代码。

<?php
 require_once __DIR__.'/vendor/autoload.php';

    session_start();

    $client = new Google_Client();
    $client->setAuthConfigFile('client_secret.json');
    $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
    $client->setScopes(array('https://www.googleapis.com/auth/drive'));

    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        $drive_service = new Google_Service_Drive($client);
        $files_list = $drive_service->files->listFiles(array())->getFiles();
        echo "<pre>";
        print_r($files_list);
    } else {
        $redirect_uri = 'http://localhost/oauth2callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }   
    $filename = "abc.txt";
    $file = new Google_Service_Drive_DriveFile();
    $file->setName($filename);
    $result = $drive_service->files->create($file, array(
  'data' => file_get_contents("abc.txt"),
  'mimeType' => 'application/octet-stream',
  'uploadType' => 'media'
));

输出是:

Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}
' in D:\xampp\htdocs\vendor\google\apiclient\src\Google\Http\REST.php:118

1 个答案:

答案 0 :(得分:-1)

错误表示The user does not have sufficient permissions for the {fileId}用户没有文件的写入权限,应用程序正在尝试修改该文件。

授予您的服务帐户访问该文件的权限。使用Permissions:insert插入文件权限。

arrToReturn