【问题标题】:How to Download G Suite docs/sheets to pdf/xls programatically?如何以编程方式将 G Suite 文档/表格下载为 pdf/xls?
【发布时间】:2021-01-21 02:53:53
【问题描述】:

我正在尝试以从 CLI 以编程方式的方式将 Google 文档下载到 PDF 或将工作表下载到 XLS。

到目前为止我尝试过的步骤:

  1. Contact support,但看不到 (?) 帮助图标
  2. Google 10 分钟...我认为 Google Drive API 会这样做(不确定)
  3. 启用Google Drive API
  4. 已注册 GCP 项目
  5. 将 UI 导航到 enable the API
  6. 使用文档 ID 字段尝试 GET API 会得到 400 Invalid field selection

我现在有点卡住了,不知道如何继续。有什么建议吗?

【问题讨论】:

  • 当我看到你的演示视频时,我认为你在 Drive API v3 中使用了“About:get”,并且文件 ID 被放到了fields。在这种情况下,就会发生这样的错误。我认为你的问题的原因是由于这个。当您想使用“Try this API”查看文件元数据时,请通过将文件 ID 设置为 fileId 来使用“Files: get”。 Ref 对了,可以提供programmatically from the CLICLI 的详细信息吗?
  • 您使用什么语言?您能否提供您正在处理的代码,特别是您提出的请求?
  • @Tanaike developers.google.com/drive/api/v3/reference/files/get WORKS.. 那么如何下载文件呢?
  • 感谢您的回复。我不得不为我糟糕的英语水平道歉。为了从 Google Drive 下载文件,有 2 个方向。 1.当文件是你的并且文件没有被公开共享时,你可以使用访问令牌下载它。 Ref 2.当文件被公开共享时,您可以不使用访问令牌下载它。 Ref 对了,可以提供programmatically from the CLICLI的详细信息吗?

标签: google-drive-api


【解决方案1】:

警告:希望 前面的文字信息墙!我还上传了完整的 Jupyter Notebook 供您克隆和运行 here,因为您已经意识到,将这类东西放在一起可能具有挑战性。


由于我们将通过 google drive API 导出文件,因此我们需要该范围的凭据,详见 https://developers.google.com/drive/api/v3/reference/files/export#auth

但是,首先我们需要选择https://developers.google.com/identity/protocols/oauth2#scenarios中详述的身份验证方法。

既然您提到了创建 GCP 项目,我假设您有兴趣使用 GCP 服务帐号 详见https://developers.google.com/identity/protocols/oauth2#serviceaccount

您可以在https://console.developers.google.com/apis/credentials 创建服务帐户 或如https://developers.google.com/identity/protocols/oauth2/service-account#creatinganaccount中所述

确保在创建该服务帐户时为其启用域范围委派,并在https://admin.google.com/ac/owl/domainwidedelegation 下授予它https://www.googleapis.com/auth/drive 范围,否则您将无法冒充其他用户(包括您自己)并下载他们的文件.

然后我们使用刚刚下载的SERVICE_ACCOUNT_FILE 和我们定义的SCOPES 创建一个Credentials 对象。

但是,您需要首先按照https://developers.google.com/drive/api/v3/quickstart/python (pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib) 为 Google API 安装 Python 绑定

这样,以下内容就足以对 API 进行身份验证了:

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'credentials.json'

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE,
                                                      scopes=SCOPES)

# Remember, you must have created credentials.json with domain-wide delegation!
credentials = credentials.with_subject('user@example.com')

# We then build a drive_v3 service using the credentials we just created

service = build('drive', 'v3', credentials=credentials)

我们可以访问files 资源,如https://developers.google.com/drive/api/v3/reference/files/get 所示,并请求user@example.com 可以访问https://docs.google.com/document/d/fileId/edit 的文件的元数据。在你的情况下fileId=141g8UkQfdMQSTfIn475gHj1ezZVV16f5ONDxpWrrvts

files = service.files()
print(service.files().get(fileId='1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8').execute())

{'kind': 'drive#file', 'id': '1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8','名称':'空', 'mimeType': 'application/vnd.google-apps.document'}

我们再次访问文件资源,但这次导出文件,详见 https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html#export 这也可以使用https://developers.google.com/drive/api/v3/manage-downloads 来实现。

https://developers.google.com/drive/api/v3/ref-export-formats 中列出了有效的 MIME 类型。

fconr = files.export(fileId='1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8',
mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document')

fcont = fconr.execute()

print('{}...'.format(fcont[:10]))

file = open("/tmp/sample.doc", "wb")
file.write(fcont)
file.close()

b'MN\xc30\x10\x85O\xc0\x1d"'...

如您所见,fcont 包含与文档对应的二进制 blob,我将显示其中的前 10 个字节。最后,将 blob 保存到 sample.doc

ls -alh1 /tmp/sample.doc

-rw-rw-r-- 1 jdsalaro jdsalaro 6,0K Jan 20 23:38 /tmp/sample.doc

如上所述,我鼓励您在创建具有域范围委派的服务帐户、将其保存到 credentials.json 并授予其 https://www.googleapis.com/auth/drive 范围后尝试使用 Jupyter notebook

【讨论】:

  • v3 export API works!我想我对身份验证感到困惑。大脑中最容易的是什么?理想情况下,我不需要服务帐户,我只想以我的身份进行身份验证。从screen shot 可以看出为什么有两个元素,API 密钥 Oauth。我两个都需要?
  • @hendry 服务帐户确实是与 Google API 交互的最简单方法。 Anything else 将要求您了解如何正确使用 Oauth2;这总是比使用服务帐户进行身份验证更复杂。对于只需要 OAuth 的导出文件,您可以去掉 API Key 对勾,自己尝试一下。
  • 但是,我强烈建议您克隆 the notebook I provided 并让它工作。您应该理解该示例,以免从安全角度或其他角度犯下代价高昂的错误。如果您确实想使用 OAuth 而不是服务帐户,the Google OAuth Playground 是一个好的开始。
  • @jdalaro 我给你发了一个截屏视频,为什么 domain-wide-delegation / owl 似乎不起作用。我想我只剩下 Oauth 了吗?
  • @hendry 服务帐户和域范围的委派也是 OAuth。不同之处在于没有用户交互。如果您编辑您的问题以提供有关您的情况、您尝试了什么、您正在使用什么代码等的更多信息,那么您更有可能获得帮助。
猜你喜欢
  • 2019-11-30
  • 2012-07-11
  • 2011-02-24
  • 1970-01-01
  • 2010-09-07
  • 2017-12-22
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
相关资源
最近更新 更多