Search code examples
google-apigoogle-photosgoogle-photos-api

Google Photos API - How to filter albums to get photos (JPEG) only while having albumId set?


I'm trying to filter contents of an album (to get photos only) coming form Google Photos API, but the docs say that:

Filters shouldn't be used in a mediaItems:search request if the albumId is set. If a filter is used when the albumId is set, an INVALID_ARGUMENT error (400) is returned.

Does this mean that I have to download all files, than filter the response my self using the MIME type? Or can it still be done directly in the request?

Thanks!

My code:

var params = JSON.parse(localStorage.getItem('oauth2-params'));
      var xhr = new XMLHttpRequest();
      xhr.open('POST',
          'https://photoslibrary.googleapis.com/v1/mediaItems:search?' +
          'access_token=' + params['access_token'] +
          '&pageSize=25' +
          '&albumId=' + albumId +
          '&pageToken=' + this.albums[albumId].photos.nextPagination);
      xhr.responseType = "json";
      xhr.onreadystatechange = (e) => {
          if (xhr.readyState === 4 && xhr.status === 200) {

            // handling the respons...


          } else if(xhr.readyState === 4) {
            console.log(xhr.status, xhr.response);
          }
        };

        xhr.send(null);

Solution

  • Unfortunately, you can't specify both a media type and album as a parameter for a search request at the moment. There's a request for this on the issue tracker here. You can 'star' the issue to be notified of any updates.

    For now, you can do the filtering on your side, without needing to download the files themselves: Check the mimeType and the mediaMetadata properties of a media item. The mimeType field indicates what kind of file it is (for example, image/jpeg or image/png). Alternatively, you can also check whether the mediaMetadata property contains a photo.