请求访问 Google 电子表格文件

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

我尝试通过 API 请求访问文件,但失败 API 结果返回错误:

Details: "[{'message': 'File not found: https://docs.google.com/spreadsheets/d/1u...4bcSqdgc/edit#gid=0.', 'domain': 'global', 'reason': 'notFound', 'location': 'fileId', 'locationType': 'parameter'}]"

但是如果我尝试在浏览器中打开此链接 - 我看到正确的消息,您无权访问文件 - “请输入消息并发布访问请求”

我在控制台中启用了以下 API: Google Drive API BigQuery API BigQuery Migration API BigQuery Storage API Cloud Datastore API Cloud Logging API Cloud Monitoring API Cloud SQL Cloud Storage Cloud Storage API Cloud Trace API Gmail API Google Cloud API Google Cloud Storage JSON API 资源设置API 服务管理API 服务使用API

代码如下:

drive_service = build('drive', 'v3', credentials=g.gcreds_scoped)

permission_request = { 'role': 'writer', 'type': 'anyone', 'allowFileDiscovery': False }

sheets_service = build('sheets', 'v4', credentials=g.gcreds_scoped)

request = drive_service.permissions().create(body=permission_request, fileId=file_url, supportsAllDrives=True, fields='id')

response = request.execute()

检查API已启用,一切似乎都很好 看起来 Google API 有未修复的错误,并且无法正常工作,或者我错过了一些事情?

python api spreadsheet google-docs google-docs-api
1个回答
0
投票

我从你的错误消息

'message': 'File not found: https://docs.google.com/spreadsheets/d/1u...4bcSqdgc/edit#gid=0.'
猜想,你可能会使用URL作为文件ID。所以,我猜测
file_url
中的
fileId=file_url
可能是
https://docs.google.com/spreadsheets/d/1u...4bcSqdgc/edit#gid=0
。如果我的理解是正确的,这就是您当前问题的原因。在这种情况下,请使用文件 ID 而不是 URL。

如果您当前的

file_url
值为
https://docs.google.com/spreadsheets/d/1u...4bcSqdgc/edit#gid=0
,请将其修改为
1u...4bcSqdgc
。请使用正确的文件 ID(在本例中为电子表格 ID。)并再次测试。

如果电子表格 URL 为

https://docs.google.com/spreadsheets/d/###/edit#gid=0
,则
###
是电子表格 ID。 参考

注:

  • 在此答案中,假设您的

    drive_service
    客户可以访问电子表格。请注意这一点。

  • 关于

    But if i try to open this link in browser - I see correct message , that you don't have access to file - "please input message and post the access request"
    ,如果您的
    drive_service
    客户端无法访问电子表格,则表明您没有权限。在这种情况下,即使上述修改反映在您的脚本中,电子表格的权限也无法更改。请注意这一点。

参考:

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