Google API 尝试将文件移动到文件夹时权限不足
Google API Insufficient Permissions when attempting to move file to folder
总的来说,我对 Google API 还很陌生,所以请多多包涵。
我有一个 PHP 脚本,我可以用它成功地创建一个 Google Sheet 并通过 Google Sheets PHP API.
创建新的 Google Sheet 后,我想将其移动到具有我希望 Sheet 具有的权限级别的文件夹(我域中的所有用户都具有link 可以 read/write sheet).
通过环顾四周,我知道这样做的方法是通过 Google 驱动器 API,而不是直接通过 Google Sheets.
我的问题是,当我尝试将刚刚创建的 Sheet 移动到我在 Google Drive 中创建的文件夹时,出现 "Insufficient Permission" 错误。
我仔细检查了 Google API 控制台中是否启用了驱动器 API。我没有设置 Drive UI 集成,因为它似乎与命令行中的脚本 运行 无关。
这是我用来尝试将 sheet 移动到新文件夹的代码:
define('SCOPES', implode(' ', array( \Google_Service_Sheets::SPREADSHEETS)));
...
$this->client = new \Google_Client();
$this->client->setApplicationName("My Application Name");
$this->client->setScopes(SCOPES);
$this->client->setAuthConfig("/full/path/to/my/client_secret.json");
...
$this->client->setAccessType('offline');
$driveService = new \Google_Service_Drive($this->client);
$emptyFileMetadata = new \Google_Service_Drive_DriveFile();
$file = $driveService->files->get(
$this->getGoogleSheetID(),
array('fields' => 'parents')
);
$previousParents = join(',', $file->parents);
$file = $driveService->files->update(
"{ID of Google Sheet I just created}",
$emptyFileMetadata,
array(
'addParents' => "{id of folder I created manually in Google Drive}",
'removeParents' => $previousParents,
'fields' => 'id, parents'
)
);
当这段代码运行时,我看到一个异常,其中包括:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
有什么想法吗?
如指南中所述,使用 Files.update 在文件夹之间移动文件至少需要以下授权范围之一:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.scripts
https://www.googleapis.com/auth/drive.metadata
总的来说,我对 Google API 还很陌生,所以请多多包涵。
我有一个 PHP 脚本,我可以用它成功地创建一个 Google Sheet 并通过 Google Sheets PHP API.
创建新的 Google Sheet 后,我想将其移动到具有我希望 Sheet 具有的权限级别的文件夹(我域中的所有用户都具有link 可以 read/write sheet).
通过环顾四周,我知道这样做的方法是通过 Google 驱动器 API,而不是直接通过 Google Sheets.
我的问题是,当我尝试将刚刚创建的 Sheet 移动到我在 Google Drive 中创建的文件夹时,出现 "Insufficient Permission" 错误。
我仔细检查了 Google API 控制台中是否启用了驱动器 API。我没有设置 Drive UI 集成,因为它似乎与命令行中的脚本 运行 无关。
这是我用来尝试将 sheet 移动到新文件夹的代码:
define('SCOPES', implode(' ', array( \Google_Service_Sheets::SPREADSHEETS)));
...
$this->client = new \Google_Client();
$this->client->setApplicationName("My Application Name");
$this->client->setScopes(SCOPES);
$this->client->setAuthConfig("/full/path/to/my/client_secret.json");
...
$this->client->setAccessType('offline');
$driveService = new \Google_Service_Drive($this->client);
$emptyFileMetadata = new \Google_Service_Drive_DriveFile();
$file = $driveService->files->get(
$this->getGoogleSheetID(),
array('fields' => 'parents')
);
$previousParents = join(',', $file->parents);
$file = $driveService->files->update(
"{ID of Google Sheet I just created}",
$emptyFileMetadata,
array(
'addParents' => "{id of folder I created manually in Google Drive}",
'removeParents' => $previousParents,
'fields' => 'id, parents'
)
);
当这段代码运行时,我看到一个异常,其中包括:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
有什么想法吗?
如指南中所述,使用 Files.update 在文件夹之间移动文件至少需要以下授权范围之一:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.scripts
https://www.googleapis.com/auth/drive.metadata