Daniel
Daniel

Reputation: 691

Python Google Photos API List Items in Album

I am having trouble extending the API calls required to list album ids to be able to list the items contained in the album. An example of what I have done to retrieve album ids appears below. Beyond that example, I include my attempt to extend this script to be able to list the items contained in a specific album.

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)

Now, for the code that I am attempting to modify from the example above to be able to list the items in a given album defined by 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()

When I attempt to print the results, I receive an error about receiving an unexpected keyword argument, albumId.

The api call is formatted differently for search, it is https://photoslibrary.googleapis.com/v1/mediaItems:search

Is the error stemming from the : mediaItems:search which invalidates the variable in Python? Or is it related to the fact that the search call is a POST rather than a GET? Or, something else?

Upvotes: 0

Views: 2949

Answers (2)

Daniel
Daniel

Reputation: 691

The mediaitems:search requires a JSON-structured body of parameters in order to restrict the returned items to a specific albumId.

Modifying the second part of the code from the original post gives the desired result.

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)

Now, collecting the data for all the other items requires incorporating the nextPageToken and looping through the results.

Upvotes: 1

sunny chidi
sunny chidi

Reputation: 94

i cant see anywhere you defined albumId in your modified code

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

the code you copied from clearly defines it

for album in results['albums']:
    albumId = album['id']
    print(albumId)

Upvotes: 0

Related Questions