我可以通过Google Docs API下载一个Google文档,而不需要OAuth同意屏幕吗?

问题描述 投票:2回答:1

在这个问题上,我一直在翻阅已经废弃的文档和过时的Stackoverflow帖子,几经循环。

我想实现的是 用户向我的系统提供了可分享的链接(就是那个带有以下内容的链接 usp=共享 内的URL)。) 当我的系统有这个功能时,它就会通过Google Docs API下载文档。我不希望用户必须通过一个授权屏幕,因为我希望我的应用程序能够 只看到单个共享文档的URL

具体来说,我想要的结果是 Google Docs API获取 函数,提供文档的原始形式。架构 和所有。

谷歌文档的API建议我可以授权 使用API密钥 但我一直没有成功。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和它的导出功能,但这提供的是 呈现格式 而不是我想要的纯JSON输出。

希望之前在Google Docs API上碰过壁的人能够帮助我解决这个问题 :)

google-docs-api
1个回答
2
投票

我相信你的目标如下。

  • 你想通过Google Docs API的get方法,将Google Document对象作为JSON数据进行检索。
  • 你想通过API密钥来实现这个目标。
  • 你想让用户使用单一的URL下载Google Document对象。
    • I want my application to only see the single shared document URL.

对于这个问题,这个答案怎么样?

问题和解决方法。

现阶段,Docs API的get方法似乎不能使用API key,即使Google文档是公开共享的。当使用API密钥向公开共享的Google文档请求Docs API的get方法时,会返回以下值。在这种情况下,已经确认API密钥可以用于Drive 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 Apps来实现你的目标。在这种情况下,Web Apps是作为一个API使用的。

使用方法

请按以下流程操作

1. 1.创建Google Apps Script的新项目。

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

如果您想直接创建,请进入以下页面 https:/script.new. 在这种情况下,如果你没有登录谷歌,就会打开登录界面。所以请登录谷歌。这样,Google Apps Script的脚本编辑器就被打开了。

2. 2.准备脚本。

请将以下脚本(Google Apps Script)复制并粘贴到脚本编辑器中。然后 请在高级谷歌服务中启用谷歌文档API。. 这个脚本是针对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. 选择 "发布"-> "部署为Web应用 "对话框。"我" 对于 "执行应用程序为:".
    • 这样一来,脚本就会以所有者的身份运行。
  3. 选择 "任何人,即使是匿名的" 对于 "谁能访问该应用程序:".
    • 在这种情况下,不需要请求访问令牌。我认为,我建议你的目标采用这种设置。
    • 当然,你也可以使用访问令牌。届时,请将此设置为 "任何人".
  4. 点击 "部署 "按钮,作为新的 "项目版本"。
  5. 自动打开 "需要授权 "的对话框。
    1. 点击 "审查权限"。
    2. 选择自己的账户。
    3. 点击 "此应用未验证 "处的 "高级"。
    4. 点击 "转到##项目名称##(不安全)"。
    5. 点击 "允许 "按钮。
  6. 点击 "确定"。
  7. 复制Web Apps的URL。就像 https://script.google.com/macros/s/###/exec.
    • 当您修改了Google Apps脚本,请重新部署为新版本。这样,修改后的脚本就会反映到Web Apps上。请注意这一点。

4. 4. 使用Web Apps运行该功能。

这是一个请求Web Apps的示例python脚本。请设置你的Web Apps 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)
  • 本例中,在Web Apps端使用了GET方法。因此,你也可以直接使用浏览器访问上述URL。

注意事项

  • 当您修改了Web Apps的脚本,请将Web Apps重新部署为新版本。这样,最新的脚本就会反映到Web Apps上。请注意这一点。

参考资料

© www.soinside.com 2019 - 2024. All rights reserved.