gpt4 book ai didi

python - mediaItems.search_next() 返回 400

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:24 25 4
gpt4 key购买 nike

无法获取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看来,这可能是问题所在,还是我在这里遗漏了一些东西?

最佳答案

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

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

除此之外,您始终希望使用 list_next,因为 search_next 不存在。

这是在我的笔记本电脑上运行的照片 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)

关于python - mediaItems.search_next() 返回 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com