python - 通过 Google Drive API 访问共享云端硬盘时出现 404 错误

标签 python python-3.x google-drive-api

我正在尝试列出共享云端硬盘的元数据。下面是代码:

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata'
]
creds = None

if os.path.exists('token.json'):
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server(port=8080)
    with open('token.json', 'w') as token:
        token.write(creds.to_json())

上面的代码有效;可以认证成功。

service = build('drive', 'v3', credentials=creds)
file_id = '0AFmX-a2BvgHBUk9PVA'
results = service.permissions().list(fileId=file_id).execute()

代码的输出如下:

Traceback (most recent call last): File "<pyshell#23>", line 1, in results = service.permissions().list(fileId=file_id).execute() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/http.py", line 935, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/0AFmX-a2BvgHBUk9PVA/permissions?alt=json returned "File not found: 0AFmX-a2BvgHBUk9PVA.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 0AFmX-a2BvgHBUk9PVA.', 'locationType': 'parameter', 'location': 'fileId'}]">

该文件确实存在。代码有错吗?我使用的权限范围正确吗?我是否需要使用服务帐户而不是 OAuth 客户端 ID?

最佳答案

我相信您的目标和目前的情况如下。

  • 您想要从共享云端硬盘检索权限列表。
  • 您可以访问共享云端硬盘。

在这种情况下,请将 supportsAllDrives 添加到请求的查询参数中,如下所示。

supportsAllDrives: Whether the requesting application supports both My Drives and shared drives. (Default: false)

来自:

results = service.permissions().list(fileId=file_id).execute()

致:

results = service.permissions().list(fileId=file_id, supportsAllDrives=True).execute()

引用:

关于python - 通过 Google Drive API 访问共享云端硬盘时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67373483/

相关文章:

python 3 : How can I align the the format of this data when it prints

python-3.x - 将日期时间转换为没有时区信息的numpy日期时间

javascript - 使用 JS 客户端使 Google Drive 的访问 token 失效

javascript - URL 缩短器 - 自动缩短 URL

python - 如何绘制直方图并在 python 中并排描述它?

python - 如何在 CentOS 上将 Python3.5.2 设置为默认 Python 版本?

python - 为什么 get_default_prefix @classmethod 而不仅仅是常规方法

python - 如何重写枚举构造函数?

javascript - Apps Script Advanced Drive API 服务 - newFile() 方法不创建文件

python - 在一定范围内的两个列表中查找元素