Google驱动器API使用PageToken

问题描述

我正在尝试访问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,)
        .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))

解决方法

我得到了同样的东西。问题是查询根本无法更改。因此,请添加下一页令牌,否则,您需要按照生成令牌时的原样发送其他所有邮件。