0
votes

When I run the following code I get this error.

{'error': {'code': 400, 'message': 'Invalid JSON payload received. Unknown name "album_id": Proto field is not repeating, cannot start list.', 'status': 'INVALID_ARGUMENT', 'details': [{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'description': 'Invalid JSON payload received. Unknown name "album_id": Proto field is not repeating, cannot start list.'}]}]}}

If I remove the "albumId": ["albumid code"] it works fine and returns

10 new items, total 10

def _actually_list_media_items(session):
    ret = []
    params = {
        'fields': 'mediaItems(id,baseUrl,filename,mimeType,productUrl),nextPageToken',
    }
    search_json = {
        "pageSize": 10,
        "albumId": ["<albumid code>"],
        "filters": {
            "includeArchivedMedia": False,
            "contentFilter": {
                "excludedContentCategories": [
                    "DOCUMENTS",
                    "RECEIPTS",
                    "SCREENSHOTS",
                    "UTILITY",
                    "WHITEBOARDS",
                ]
            },
            "mediaTypeFilter": {
                "mediaTypes": [
                    "PHOTO",
                ],
            },
        },
    }

    tmp = 0    
    while tmp < 1:
        rsp = session.post(
            'https://photoslibrary.googleapis.com/v1/mediaItems:search',
            params=params,
            json=search_json,
        ).json()
        if 'error' in rsp:
            print(rsp)

        cur = [m for m in rsp.get('mediaItems', [])]
        ret += cur
        print(f'{len(cur)} new items, total {len(ret)}')

        pageToken = rsp.get('nextPageToken')
        if pageToken is None:
            break
        params['pageToken'] = pageToken
        tmp = tmp + 1
    return ret
2

2 Answers

2
votes

The comment about albumId and filters being exclusive is correct, so you need to pick one or the other. However, assuming you want to use the albumId by itself, you need to remove the square brackets around your albumid code, here's a clip from my code:

searchbody = {
    "albumId": album_id,
    "pageSize": 10
}
print(searchbody)

mediaresults = gAPIservice.mediaItems().search(body=searchbody).execute()
mediaitems = mediaresults.get('mediaItems', [])
for item in mediaitems:
    print(u'{0} ({1})'.format(item['filename'], item['id']))
0
votes

Edit: Apparently you can't use albumId and filters together: source

filters: object(Filters)

Filters to apply to the request. Can't be set in conjunction with an albumId.


Aside from that, albumId is a supposed to be a string not an array: source

"albumId": "<albumid code>",