Python 404通过Google drive API访问共享驱动器时出错
我正在尝试列出共享驱动器的元数据。代码如下:Python 404通过Google drive API访问共享驱动器时出错,python,python-3.x,google-drive-api,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.crede
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()
代码的输出如下所示:
回溯(最近一次调用):文件“”,第1行,在
结果=service.permissions().list(fileId=file\u id).execute()文件
“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site packages/googleapiclient/_helpers.py”,
第134行,在位置_包装中
返回包装的(*args,**kwargs)文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site packages/googleapiclient/http.py”,
执行中的第935行
引发HttpError(resp,content,uri=self.uri)GoogleAppClient.errors.HttpError:
这个文件肯定存在。代码错了吗?我是否使用了正确的权限范围?我是否需要使用服务帐户而不是OAuth客户端ID?我相信您的目标和当前情况如下
- 您想从共享驱动器检索权限列表
- 您可以访问共享驱动器
supportsAllDrives
,如下所示
supportsAllDrives
:请求应用程序是否同时支持我的驱动器和共享驱动器。(默认值:false)
发件人:
致:
参考:
results = service.permissions().list(fileId=file_id).execute()
results = service.permissions().list(fileId=file_id, supportsAllDrives=True).execute()