0

我无法扩展列出专辑 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 的事实有关?或者是其他东西?

4

2 回答 2

1

mediaitems:search需要一个结构化的JSON参数体,以便将返回的项目限制为特定的专辑 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 = ""
body = {
    "albumId": [AlbumId],
    "pageSize": 10
    }
results = service.mediaItems().search(body=body).execute()
print(results)

现在,收集所有其他项目的数据需要合并nextPageToken和循环结果。

于 2018-11-16T01:27:54.907 回答
0

我看不到您在修改后的代码中定义专辑 ID 的任何地方

results = service.mediaItems().search(pageSize=10,**albumId=albumId**).execute()

您复制的代码清楚地定义了它

for album in results['albums']:
    albumId = album['id']
    print(albumId)
于 2018-11-15T21:34:03.970 回答