mediaItems.search_next()返回400

时间:2019-05-24 14:18:00

标签: python google-api-python-client google-photos google-photos-api

无法获得mediaItems.search的所有结果:

photos = google.get_service(credentials, 'photoslibrary', 'v1')
request = photos.albums().list(pageSize=50)
while request is not None:
    result = request.execute()
    for album in result['albums']:
        request2 = photos.mediaItems().search(body={'albumId': album['id']})
        while request2 is not None:
            result2 = request2.execute()
            request2 = photos.mediaItems().search_next(request2, result2)
            print('nextpageToken' in result2, request2)
    request = photos.albums().list_next(request, result)

在使用的第一个search_next()调用中,运行此操作失败

[...]
  File "/usr/local/lib/python3.7/dist-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/googleapiclient/http.py", line 851, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://photoslibrary.googleapis.com/v1/mediaItems:search?alt=json returned "Invalid JSON payload received. Unexpected end of string. Expected an object key or }.

看来library isn't really supported是问题所在,还是我在这里错过了什么?

2 个答案:

答案 0 :(得分:0)

google-api-python-client是所有Google's discovery based APIs的通用客户端,因此它支持基于该协议的所有API,包括照片协议。

使用服务的正确方法是调用build method,然后使用服务的可用方法。

除此之外,您总是要使用list_next,因为search_next不存在。

以下是在笔记本电脑上运行的photos API示例(python 3.6)

import os
import pickle

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

SCOPES = ['https://www.googleapis.com/auth/photoslibrary.readonly', ]


# we check if we save the credentials in the past and we reuse them
if not os.path.exists('credentials.dat'):

    # no credentials found, we run the standard auth flow
    flow = InstalledAppFlow.from_client_secrets_file('client_id.json', SCOPES)
    credentials = flow.run_local_server()

    with open('credentials.dat', 'wb') as credentials_dat:
        pickle.dump(credentials, credentials_dat)
else:
    with open('credentials.dat', 'rb') as credentials_dat:
        credentials = pickle.load(credentials_dat)

if credentials.expired:
    credentials.refresh(Request())

photos_sdk = build('photoslibrary', 'v1', credentials=credentials)

# photos API
photos_albums_api = photos_sdk.albums()
photos_mediaitems_api = photos_sdk.mediaItems()

albums_list_params = {
    'pageSize': 50,
}

# first request
albums_list_req = photos_albums_api.list(**albums_list_params)

while albums_list_req is not None:
    photos_albums_list = albums_list_req.execute()

    # print(photos_albums_list)

    for album in photos_albums_list['albums']:
        print(album['title'])

        mediaitems_search_req = photos_mediaitems_api.search(body={'albumId': album['id']})

        while mediaitems_search_req is not None:
            mediaitems_search = mediaitems_search_req.execute()

            print(mediaitems_search)

            # mediaItems pagination management
            mediaitems_search_req = photos_mediaitems_api.list_next(mediaitems_search_req, mediaitems_search)

    # albums pagination handling
    albums_list_req = photos_albums_api.list_next(albums_list_req, photos_albums_list)

答案 1 :(得分:0)

如果结果多于指定的pageSize,则API返回pageToken,您应使用它来请求下一部分。在此处查看示例Access Google Photo API with Python using google-api-python-client