在这个问题上,我一直在翻阅已经废弃的文档和过时的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上碰过壁的人能够帮助我解决这个问题 :)
我相信你的目标如下。
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使用的。
请按以下流程操作
Web Apps的示例脚本是一个Google Apps脚本,所以请创建一个Google Apps脚本项目。所以请创建一个Google Apps Script的项目。
如果您想直接创建,请进入以下页面 https:/script.new. 在这种情况下,如果你没有登录谷歌,就会打开登录界面。所以请登录谷歌。这样,Google Apps Script的脚本编辑器就被打开了。
请将以下脚本(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);
}
https://script.google.com/macros/s/###/exec
.这是一个请求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)