curl - 如何通过 Google Drive 的 API 访问与 Google API Explorer 相同的文件列表?

标签 curl google-api google-drive-api service-accounts

使用 Google Drive 的 API,我可以获得授权和 token ,但在尝试获取文件列表时,API 目前仅返回一项:名为“入门”的 PDF。据推测,这是刚刚建立的 Google 云端硬盘中的默认项目,该项目早已从我们正在使用的预期 Google 云端硬盘帐户中删除了。

同时,使用 API Explorer ( https://developers.google.com/apis-explorer ) 会返回数百个项目,这些项目实际上代表了相关帐户的 Google 云端硬盘。

API Explorer 表示它发送的请求是:

GET https://www.googleapis.com/drive/v3/files?corpus=user&pageSize=100&key={YOUR_API_KEY}

发送直接返回“权限不足”错误,这是有道理的:是 api_key,而不是 access_token 将 &key=&access_token= 切换确实会返回文件列表,但其中唯一的项目是上面提到的“入门”PDF。

API 和 Explorer 之间的差异给人的印象是......

a) 每个都引用不同的帐户,或者

b) API 控制台 ( https://console.developers.google.com ) 中授予的权限/范围/访问权限比 Explorer 窄

关于 a) 每个引用不同的帐户:不太可能。这些凭据都是在登录到用于运行 API Explorer 的同一 Google 帐户时在 API 控制台中创建的。

关于 b) 权限/范围/访问权限在某种程度上有所不同:可能性更大,但到目前为止,如何调整或调整哪些内容以产生影响还不是很明显。

发送的 cURL 调用:

curl https://www.googleapis.com/drive/v3/files?corpus=user&pageSize=100&key={YOUR_API_KEY} (NG.  Needs a token)
curl https://www.googleapis.com/drive/v3/files?corpus=user&pageSize=100&access_token={recently_obtained_token} (this works, but returns only one item -- the "Getting Started" PDF)

返回:

{"kind": "drive#fileList", "incompleteSearch": false, "files": [{"kind": "drive#file", "id": "XXXX", "name": "Getting started", "mimeType": "application/pdf"}]}

此处列出了用于实现此目的的授权和 token 步骤... How to connect to the Google Drive API using cURL?

根据这篇文章(Access Google Drive API Returns Empty File List) 使用的范围确实是 https://www.googleapis.com/auth/drive 在同一链接下,kvzrock 的一个回答建议给予“服务帐户的电子邮件地址许可” 几个链接:

Google Drive API, Oauth and service account

How do I authorise an app (web or installed) without user intervention?

但还没有理解这些。

最佳答案

听起来您已经使用了服务帐户,因此您正在使用的凭据以及您看到的一个文件属于服务帐户,而不是您的用户帐户。一些确认方法:-

  1. 检查文件的创建日期。如果它与您生成凭据的日期相同,则它是一个服务帐户。
  2. 检查文件的所有者。
  3. 获取“关于”资源 https://developers.google.com/drive/api/v3/reference/about

不幸的是,Google 文档让人们得出这样的结论:服务帐户是绕过 OAuth 来访问您的文件的便捷方式,而它应该用大红字表示“服务帐户不是”您认为存在的帐户!!!”

您有 2 个选择:- 1. 与服务帐户共享您要访问的所有文件 1. 停止使用服务帐户并按照您发布的最后一个链接 ( How do I authorise an app (web or installed) without user intervention? ) 中的步骤直接访问您的常规帐户

关于curl - 如何通过 Google Drive 的 API 访问与 Google API Explorer 相同的文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393084/

相关文章:

c# - 如何列出特定文件夹中的文件夹和文件

javascript - Angular 2+ HTTP POST 和 GDrive API。带名称的断点续传文件上传

google-drive-api - 无法在 API 控制台中向 Google Drive 注册应用程序

php - 使用纯 PHP 在现有的 Google 电子表格中插入行

php - 错误:未能找到建议检查集群运行状况的数据节点

php - 如何在使用 php curl 发布数据时为每个请求设置不同的 ip 地址

curl - 如何使用curl从pod内访问Kubernetes API?

post - 优步API |为 Ride 请求请求访问 token 返回 'invalid_grant' 错误

javascript - 如何从 google map api 更改 map 位置?

python - Nosetest 和 Google API : "no module named apiclient.discovery"