python - Google drive API 在使用 PageToken 时返回 400 Bad request
问题描述
我正在尝试访问 google drive API v3 中的下一页文件。但是这个特定的片段失败了。
我查看了https://stackoverflow.com/a/38479234/11705040,但这篇文章的查询query_string="'{0}' in parents".format(item["id"])
似乎很好。
temp = (
self.service.files().list(
q=query_string,
pageToken=nextPageToken,
pageSize=5,
fields="nextPageToken, files(id, name, mimeType, size, parents, modifiedTime)",
).execute()
)
错误是这样的:
File "D:\Mac\Sites\project-reconnaissance\cronjob\env\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "D:\Mac\Sites\project-reconnaissance\cronjob\env\lib\site-packages\googleapiclient\http.py", line 907, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=%271a49i6ivoZ_ErAin7KvvG9VBZOyeeImqc%27+in+parents&pageToken=~%21%21~AI9FV7TwnBwo_MYMR8Rap94OgbFCPDWcy3n6C7prOb_V7MjhT6tjVHHP58QBSyROFNI4HtgmMW6o1AznGcmN1yF_wKr5SSlSgiDocew78RYuyZXDdGO6WOnhrkNeuYW3RyD1nSSWKXNro-skWfZgMuKpQ9P1QfnAxQbdiXAVQ8lO6J2b_xzwpMhAjLWV1kOMCsTqVP9wI61SYJGa1qRJwhUzwL6OEaqffuhXaH3Aa6aXUWg6aIwzAYqGuwTDu1S9BIAqSE3qXDKpRebmvC-fXz0iiDlqlPsuJA-MWuzSNIk0_XlgRpRavrGN9c5miN32i9JML6VQNKs9c_mdb3Ggwzm8KUlPO8eeYg8rdM9gwg6asch22HjLNeKc5kqIRjG9OjVA0RVovpvE&pageSize=5&fields=nextPageToken%2C+files%28id%2C+name%2C+mimeType%2C+size%2C+parents%2C+modifiedTime%29&alt=json returned "Invalid Value">
编辑 添加最小可重现示例:当我尝试在子文件夹中运行查询时遇到此问题
import os
import pickle
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
ID = "1a49i6ivoZ_ErAin7KvvG9VBZOyeeImqc"
SCOPES = ["https://www.googleapis.com/auth/drive.readonly"]
def get_gdrive_service():
"""Gets the service object of google drive"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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=0)
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
# return Google Drive API service
return build("drive", "v3", credentials=creds)
print("connecting to google drive...")
service = get_gdrive_service()
print("connected to google drive!")
def list_files(query_string, nextPageToken=None):
"""List the google drive files as returned by Google Drive API."""
# Get the first set of results
# BUG here when coming from line 68
items = (
service.files().list(
q=query_string,
pageToken=nextPageToken,
pageSize=5,
fields="nextPageToken, files(id, name, mimeType, size, parents, modifiedTime)",
)
.execute()
)
if items['files'] != []:
print("get first set of results", items)
nextPageToken = items.get("nextPageToken")
items = items.get("files", [])
# Do this for all internal folders as well
for item in items:
print("Doing item:", item["name"])
# This works fine
list_files(
query_string="'{0}' in parents".format(item["id"])
)
# If there is a nextPage Token, go further to get next set of items
if nextPageToken:
print("Fetching next set of results")
q = "'{0}' in parents".format(ID)
# SOME BUG in line 50 when going from here, fails with HTTP 400
list_files(q, nextPageToken=nextPageToken)
list_files("'{0}' in parents".format(ID))
解决方案
我得到了同样的东西。问题是查询根本无法更改。所以添加下一页令牌,否则您需要完全按照生成令牌时的方式发送其他所有内容。
推荐阅读
- r - 建立 3 个不同的多元回归模型来预测
- javascript - 使用 SQL 表中的 SELECT 更新 Datepicker UI javascript 文件?
- javascript - 在 NodeJS 中模仿高 CPU 和内存使用率
- flutter - 如何在 Flutter 中将图像插入到类属性中
- python - 是否有在 Python 中使用内联 C 代码的内置方法?
- python - 如何在 Python 中的一列数据框中找到某些内容的百分比?
- unity3d - 睡眠运动刚体不调用 OnCollisionStay
- r - R 脚本,用于确定来自 PCR 运行的任何给定样本的滴度值
- c# - 有没有办法在 C# 中生成函数重载?
- angular - Ionic/Angular 路由器无法正确重定向