我可以在没有 OAuth 同意屏幕的情况下通过 Google 文档 API 下载 Google 文档吗?

Can I download a Google Document, via the Google Docs API without an OAuth consent screen?

我已经在这个问题上循环浏览过时的文档和过时的 Whosebug 帖子。

我想要实现的目标:用户向我的系统提供可共享 link(URL 中带有 _usp=sharing_ 的那个)。当我的系统有这个时,它会通过 Google Docs API 下载文档。我不希望用户必须通过授权屏幕,因为我希望我的应用程序 只能看到单个共享文档 URL

具体来说,我想要 Google Docs API get function, which provides the document in its raw form, structure 和所有结果。

Google 文档 API 建议我可以授权 using an API key 但我没有成功。 API 抱怨我的请求缺少凭据。最简单形式的调用看起来像这样:

service = build('docs', 'v1', developerKey={key from dashboard})
query = service.documents.get(documentId={my doc id from shared url})
response = query.execute()

其他帖子指出了 Google Drive API 及其导出功能,但这提供了 rendered formats 而不是我正在寻找的纯 JSON 输出。

希望以前对 Google 文档 API 大加抨击的人可以帮助解决这个问题 :)

我相信你的目标如下。

  • 您想使用 Google Docs API.
  • 的 get 方法将 Google 文档对象检索为 JSON 数据
  • 您想使用 API 键实现此目的。
  • 您想让用户使用单个 URL 下载 Google 文档对象。
    • 来自 I want my application to only see the single shared document URL.

为此,这个答案怎么样?

问题和解决方法:

在当前阶段,似乎 Docs API 的 get 方法不能与 API 键一起使用,即使 Google 文档是公开共享的。当使用 API 键向公开共享的 Google 文档请求 Docs API 的 get 方法时,返回以下值。 (在这种情况下,已经确认API键可以用于驱动器API。)

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

从这个结果,发现API键不能用于这种情况。为了避免这种情况,可以考虑使用服务帐户。但是在当前阶段,访问令牌不能用作查询参数。 所以需要考虑其他解决方法。

因此,作为另一种解决方法,我想建议使用由 Google Apps Script 创建的 Web 应用程序来实现您的目标。在这种情况下,Web 应用程序用作 API.

用法:

请执行以下流程。

1。创建 Google Apps 脚本的新项目。

Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。

如果要直接创建,请访问https://script.new/。在这种情况下,如果您未登录 Google,则会打开登录屏幕。所以请登录Google。这样,Google Apps Script 的脚本编辑器就打开了。

2。准备脚本。

请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。还有please enable Google Docs API at Advanced Google services。此脚本用于 Web 应用程序。

function doGet(e) {
  const key = "samplekey";  // Please set the key for using this Web Apps.

  const k = e.parameter.key;
  const documentId = e.parameter.documentId;
  if (k === key) {
    const obj = Docs.Documents.get(documentId, {fields: "*"});
    return ContentService.createTextOutput(JSON.stringify(obj)).setMimeType(ContentService.MimeType.JSON);
  }
  return ContentService.createTextOutput(JSON.stringify({error: "Wrong key."})).setMimeType(ContentService.MimeType.JSON);
}
  • 本例中使用的是GET方式

3。部署 Web 应用程序。

  1. 在脚本编辑器上,通过"Publish" -> "Deploy as web app"打开一个对话框。
  2. Select "Me" 对于 "Execute the app as:"
    • 至此,脚本运行作为所有者。
  3. Select "Anyone, even anonymous" 对于 "Who has access to the app:"
    • 在这种情况下,请求不需要访问令牌。我想我推荐这个设置来实现你的目标。
    • 当然,你也可以使用access token。届时请设置为"Anyone".
  4. 单击 "Deploy" 按钮作为新按钮 "Project version"。
  5. 自动打开"Authorization required"的对话框。
    1. 点击"Review Permissions"。
    2. Select自己的账号。
    3. 在 "This app isn't verified" 单击 "Advanced"。
    4. 点击"Go to ### project name ###(unsafe)"
    5. 单击 "Allow" 按钮。
  6. 点击"OK"。
  7. 复制 Web 应用程序的 URL。就像 https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

4。 运行 使用 Web Apps 的功能。

这是用于请求 Web 应用程序的示例 python 脚本。请设置您的网络应用 URL 和 documentId.

import requests
document_id = '###'  # Please set the Document ID.
key = 'samplekey'  # Please set the key for using Web Apps.
url = 'https://script.google.com/macros/s/###/exec?documentId=' + document_id + '&key=' + key
res = requests.get(url)
print(res.text)
  • 在这种情况下,GET方法用于Web Apps端。所以你也可以直接用浏览器访问上面的URL

注:

  • 当您修改了Web Apps的脚本后,请将Web Apps重新部署为新版本。由此,最新的脚本被反​​映到Web Apps。请注意这一点。

参考文献: