0

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?

Daniel
  • 691
  • 2
  • 8
  • 19

2 Answers2

1

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.

Daniel
  • 691
  • 2
  • 8
  • 19
0

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)
sunny chidi
  • 84
  • 2
  • 5
  • you're right - I missed that, the original will be updated soon. Thanks for the catch – Daniel Nov 15 '18 at 21:36
  • you'll probably make two calls, one to grab the id and the second to loop through – sunny chidi Nov 15 '18 at 21:41
  • I agree - I am seeking help on the 2nd call. It employs a different service from the one used to list album ids and I believe I am not setting that service correctly. – Daniel Nov 15 '18 at 21:46