我可以在没有OAuth同意屏幕的情况下通过Google Docs API下载Google文档吗? [英] Can I download a Google Document, via the Google Docs API without an OAuth consent screen?
问题描述
关于这个问题,我已经遍历了几个循环,遍历了不赞成使用的文档和过时的Stackoverflow帖子.
I've been going in a few loops through deprecated documentation and outdated Stackoverflow posts on this problem.
我要实现的目标:用户为我的系统提供了可共享的链接(URL中带有 _usp = sharing _
的链接).当我的系统具有此功能时,它将通过Google Docs API下载文档.我不希望用户必须经过授权屏幕,因为我希望我的应用程序仅看到单个共享文档的网址
What I want to achieve: User provides my system the Shareable link (The one with _usp=sharing_
within the URL). When my system has this, it downloads the document via the Google Docs API. I don't want the user to have to go through an authorization screen because I want my application to only see the single shared document URL
具体来说,我想要 Google Docs API的结果get 函数,以原始格式提供文档,结构和所有.
Specifically, I want the results of the Google Docs API get function, which provides the document in its raw form, structure and all.
Google Docs API建议我可以使用API密钥授权 但我没有成功.API抱怨我的请求缺少凭据.最简单形式的呼叫如下所示:
The Google Docs API suggests I can authorize using an API key but I have not been successful. The API complains that my request is missing credentials. The call in its simplest form looks something like so:
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及其导出功能,但这提供了
Other posts have pointed to the Google Drive API and its export function but this provides rendered formats rather than that pure JSON output I'm looking for.
希望以前曾对Google Docs API投入思想的人可以帮助解决这个问题:)
Hoping someone who's bashed their head against the Google Docs API previously can help with this one :)
推荐答案
我相信您的目标如下.
- 您要使用Google Docs API的get方法以JSON数据的形式检索Google Document对象.
- 您要使用API密钥来实现.
- 您想让用户使用单个URL下载Google Document对象.
- 从
我希望我的应用程序仅看到单个共享文档的URL
.
为此,这个答案如何?
在当前阶段,即使Google文档是公开共享的,Docs API的get方法似乎也无法与API密钥一起使用.使用API密钥向公开共享的Google文档请求Docs API的get方法时,将返回以下值.(在这种情况下,已经确认API密钥可用于Drive API.)
In the current stage, it seems that the get method of Docs API cannot be used with the API key, even when the Google Document is publicly shared. When the get method of Docs API is requested to the publicly shared Google Document with the API key, the following value is returned. (In this case, it has already been confirmed that the API key can be used for 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密钥.为了避免这种情况,可以考虑使用服务帐户.但是在当前阶段,访问令牌不能用作查询参数.参考因此,需要考虑其他解决方法.
From this result, it is found that the API key cannot be used for this situation. In order to avoid this, it can consider about the use of the service account. But in the current stage, the access token cannot be used as the query parameter. Ref So it is required to think of other workaround.
因此,作为另一个解决方法,我建议使用Google Apps脚本创建的Web Apps来实现您的目标.在这种情况下,Web Apps用作API.
So, as another workaround, I would like to propose to use Web Apps created by Google Apps Script for achieving your goal. In this case, the Web Apps is used as an API.
请执行以下流程.
Web Apps的示例脚本是Google Apps脚本.因此,请创建一个Google Apps脚本项目.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
如果您想直接创建它,请访问 https://script.new/.在这种情况下,如果您未登录Google,则会打开登录"屏幕.因此,请登录到Google.这样,将打开Google Apps脚本的脚本编辑器.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
请复制以下脚本(Google Apps脚本)并将其粘贴到脚本编辑器中.并且 请在高级Google服务中启用Google Docs API .该脚本适用于Web Apps.
Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Docs API at Advanced Google services. This script is for the Web Apps.
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方法.
- 在脚本编辑器上,通过发布"->作为Web应用程序部署"打开一个对话框.
- 为以以下方式执行应用":选择我" .
- 通过这种方式,脚本以所有者身份运行.
- 在这种情况下,不需要请求访问令牌.我认为我建议您将此设置用于您的目标.
- 当然,您也可以使用访问令牌.那时,请将其设置为任何人" .
- 点击查看权限".
- 选择自己的帐户.
- 在此应用未验证"中单击高级".
- 点击转到###项目名称###(不安全)"
- 单击允许"按钮.
- 从
- 单击确定".
- 复制Web应用程序的URL.就像
https://script.google.com/macros/s/###/exec
.- 修改Google Apps脚本后,请重新部署为新版本.这样,修改后的脚本将反映到Web Apps.请注意这一点.
4.使用Web Apps运行该功能.
这是用于请求Web应用程序的示例python脚本.请设置您的Web Apps URL和documentId.
4. Run the function using Web Apps.
This is a sample python script for requesting Web Apps. Please set your Web Apps URL and 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应用程序的脚本后,请重新部署Web应用程序为新版本.这样,最新脚本将反映到Web应用程序中.请注意这一点.
这篇关于我可以在没有OAuth同意屏幕的情况下通过Google Docs API下载Google文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!