【发布时间】:2018-03-27 22:20:56
【问题描述】:
总的来说,我对 Google API 还是很陌生,所以请多多包涵。
我有一个 PHP 脚本,我可以使用它成功地创建一个 Google Sheet 并通过 Google Sheets PHP API 对其进行操作。
创建新的 Google 表格后,我想将其移动到具有我希望表格拥有的权限级别的文件夹(我的域中所有知道该链接的用户都可以读/写表格)。
通过环顾四周,我了解到执行此操作的方法是通过 Google Drive API,而不是直接通过 Google Sheets。
我的问题是,当我尝试将刚刚创建的工作表移动到我在 Google 云端硬盘中创建的文件夹中时,出现“权限不足”错误。
我已在 Google API 控制台中仔细检查了 Drive API 是否已启用。我没有设置 Drive UI 集成,因为这似乎与从命令行运行的脚本无关。
这是我用来尝试将工作表移动到新文件夹的代码:
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"
}
}
有什么想法吗?
【问题讨论】:
-
嗯...您的帐户、Google 表格和文件夹有什么权限?
-
我创建了两者,所以我假设我对两者都有写权限。
-
当我查看文件夹的详细信息时,它正确地显示“{my domain} 中知道链接的任何人都可以编辑。”
标签: php google-drive-api google-sheets-api