【问题标题】:Google API Insufficient Permissions when attempting to move file to folder尝试将文件移动到文件夹时 Google API 权限不足
【发布时间】: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


【解决方案1】:

如指南中所述,使用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

【讨论】:

  • 我交给 setScopes() 的范围字符串现在是“googleapis.com/auth/spreadsheetsgoogleapis.com/auth/drive”,但我仍然遇到同样的错误。 (看起来 stackoverflow 没有显示 https:// 部分)
  • 我认为您正在使用 Drive API,将 Drive 文件移动到新文件。尝试仅使用“googleapis.com/auth/drive”作为您的范围。
  • 为了尝试一下,我添加了 $this->client->setScopes(\Google_Service_Drive::DRIVE);就在 $driveService = new \Google_Service_Drive($this->client); 之前它导致了同样的错误。
  • 事实证明正确设置范围是问题所在。我更改了脚本中的范围字符串,但未能刷新我的凭据。一旦我刷新了我的凭据,它就起作用了。感谢您的帮助!
猜你喜欢
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2021-03-23
  • 2013-03-01
  • 1970-01-01
相关资源
最近更新 更多