1

无法获取 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 }.

那个似乎并没有得到真正的支持,所以这可能是问题所在,或者我在这里遗漏了什么?

4

2 回答 2

1

google-api-python-client 是Google 所有基于发现的 API 的通用客户端,因此它支持基于此协议的所有 API,包括照片之一。

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

除此之外,您总是想使用list_nextas search_nextdoes not exist。

这是在我的笔记本电脑上运行的照片 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)
于 2019-05-26T19:48:26.500 回答
0

如果结果多于指定pageSize的 ,API 会返回一个pageToken,您应该使用它来请求下一部分。请参阅此处的示例使用 google-api-python-client 使用 Python 访问 Google Photo API

于 2019-06-21T18:45:21.947 回答