我无法扩展列出专辑ID所需的API调用,以便能够列出专辑中包含的项目。下面显示了我为检索专辑ID所做的操作的一个示例。除了该示例之外,我还尝试扩展此脚本以能够列出特定专辑中包含的项目。
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))
hasNextPageToken = False
nextPageToken = ""
results = service.albums().list(pageSize=10).execute()
if 'nextPageToken' in results:
hasNextPageToken = True
nextPageToken = results['nextPageToken']
for album in results['albums']:
albumId = album['id']
print(albumId)
现在,对于我尝试从上面的示例中修改的代码,以便能够列出albumId
定义的给定相册中的项目:
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))
hasNextPageToken = False
nextPageToken = ""
albumId = [albumIdFromPreviousStep]
results = service.mediaItems().search(pageSize=10,albumId=albumId).execute()
当我尝试打印results
时,收到关于接收意外关键字参数albumId
的错误。
api调用的搜索格式不同,它是https://photoslibrary.googleapis.com/v1/mediaItems:search
错误是否源自:mediaItems:search
,它使Python中的变量无效?还是与搜索调用是POST而不是GET有关?还是其他?
答案 0 :(得分:1)
mediaitems:search
需要一个JSON
结构的参数主体,才能将返回的项目限制为特定的AlbumId。
修改原始帖子中的第二部分代码将获得所需的结果。
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.discovery import build
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))
hasNextPageToken = False
nextPageToken = ""
body = {
"albumId": [AlbumId],
"pageSize": 10
}
results = service.mediaItems().search(body=body).execute()
print(results)
现在,要收集所有其他项目的数据,则需要合并nextPageToken
并遍历结果。
答案 1 :(得分:0)
我看不到在修改后的代码中定义了albumId的任何地方
results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()
您从中复制的代码清楚地定义了它
for album in results['albums']:
albumId = album['id']
print(albumId)