List library contents, albums, and media items

Required authentication scope

The Google Photos Library API contains multiple scopes used to access media items and albums. The media items returned from various calls are different based on which scopes have been requested by the developer.

The .readonly scope allows access to all media items in the user's library. The .readonly.appcreateddata scope allows access only to media items that were created by the app. For more information, see Authentication and authorization scopes.

Overview

An important use of the API is to list media items that the user has backed up to Google Photos. Items in a particular album or from the user's entire library (the default view in the Google Photos app) can be listed.

You can use filters to select photos that match a specified date, content category, or media type when you list items from the user's library. This feature isn't supported when you list items from albums.

Listing the library and album contents returns a list of media items. Enrichments that are part of an album aren't included. Media items describe a photo, video, or other media. A mediaItem includes a direct link to the item, a link to the item in Google Photos, and other relevant metadata. For more information, see Access media items and mediaItems.

Listing albums

You can retrieve a list of the user's albums using albums.list.

REST

Here is a sample request:

GET https://photoslibrary.googleapis.com/v1/albums

The request returns the following result:

{
  "albums": [
    {
      "id": "album-id",
      "title": "album-title",
      "productUrl": "album-product-url",
      "coverPhotoBaseUrl": "album-cover-base-url_do-not-use-directly",
      "coverPhotoMediaItemId": "album-cover-media-item-id",
      "isWriteable": "whether-you-can-write-to-this-album",
      "mediaItemsCount": "number-of-media-items-in-album"
    },
    ...
  ],
  "nextPageToken": "token-for-pagination"
}

Java

try {
  // Make a request to list all albums in the user's library
  // Iterate over all the albums in this list
  // Pagination is handled automatically
  ListAlbumsPagedResponse response = photosLibraryClient.listAlbums();

  for (Album album : response.iterateAll()) {
    // Get some properties of an album
    String id = album.getId();
    String title = album.getTitle();
    String productUrl = album.getProductUrl();
    String coverPhotoBaseUrl = album.getCoverPhotoBaseUrl();
    // The cover photo media item id field may be empty
    String coverPhotoMediaItemId = album.getCoverPhotoMediaItemId();
    boolean isWritable = album.getIsWriteable();
    long mediaItemsCount = album.getMediaItemsCount();
  }

} catch (ApiException e) {
  // Handle error
}

PHP

try {
    // Make a request to list all albums in the user's library
    // Iterate over all the albums in this list
    // Pagination is handled automatically
    $response = $photosLibraryClient->listAlbums();
    foreach ($response->iterateAllElements() as $album) {
        // Get some properties of an album
        $albumId = $album->getId();
        $title = $album->getTitle();
        $productUrl = $album->getProductUrl();
        $coverPhotoBaseUrl = $album->getCoverPhotoBaseUrl();
        // The cover photo media item id field may be empty
        $coverPhotoMediaItemId = $album->getCoverPhotoMediaItemId();
        $isWriteable = $album->getIsWriteable();
        $totalMediaItems = $album->getTotalMediaItems();
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

Each returned album has an ID that can be used to retrieve the contents of the album as shown in Listing album contents. It also includes the title and the number of media items it contains.

The productUrl points to the album in Google Photos that can be a opened by the user.

The coverPhotoMediaItemId contains the media item ID that represents the cover photo of this album. To the access this cover image, use coverPhotoBaseUrl. You shouldn't use the coverPhotoBaseUrl directly without specifying additional parameters.

Albums that have been created in the user's account or added to the user's account and that your app has shared include an additional shareInfo property. For more details, see Share media.

Albums may also have an isWriteable flag to indicate if you can create media items in the album. This flag defaults to false if it is not returned. It is dependent on the access granted to your application, including the following:

  • Who created the album.
  • If it's shared or not.
  • What scopes the user has approved.

This flag may change if any of these criteria change. For more details, see Create albums. The response also contains a nextPageToken. For more information, see Pagination.

The response for empty albums varies in that, mediaItemsCount and coverPhotoMediaItemId are set to 0 by default and are omitted from the REST response. Also note that coverPhotoBaseUrl points to a default placeholder image.

Listing library contents

You can list all the media items from the user's Google Photos library. It excludes archived and deleted items. You can list media items based on their content, date, and other properties by applying filters. If the user has not added an item available in the Sharing tab of their Google Photos to their library, that item is not included in this list.

To retrieve a media item, call mediaItems.list.

REST

Here is a sample request:

GET https://photoslibrary.googleapis.com/v1/mediaItems
Content-type: application/json
Authorization: Bearer oauth2-token
{
  "pageSize": "100",
}

The GET request returns the following response:

{
  "mediaItems": [
    ...
  ],
  "nextPageToken": "token-for-pagination"
}

Java

try {
  // Make a request to list all media items in the user's library
  // Iterate over all the retrieved media items
  // Pagination is handled automatically
  ListMediaItemsPagedResponse response = photosLibraryClient.listMediaItems();
  for (MediaItem item : response.iterateAll()) {
    // Get some properties of a media item
    String id = item.getId();
    String description = item.getDescription();
    String mimeType = item.getMimeType();
    String productUrl = item.getProductUrl();
    String filename = item.getFilename();
  }
} catch (ApiException e) {
  // Handle error
}

PHP

try {
    // Make a request to list all media items in the user's library
    // Iterate over all the retrieved media items
    // Pagination is handled automatically
    $response = $photosLibraryClient->listMediaItems();
    foreach ($response->iterateAllElements() as $item) {
        // Get some properties of a media item
        /* @var $item \Google\Photos\Library\V1\MediaItem */
        $id = $item->getId();
        $description = $item->getDescription();
        $mimeType = $item->getMimeType();
        $productUrl = $item->getProductUrl();
        $filename = $item->getFilename();
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

The response contains a list of media items, ordered from most to least recent. For more information, see mediaItems. It also contains a nextPageToken, which is described in more detail in Pagination.

Listing album contents

To list all of the media items in an album, add the albumId field to your search request. For more information about albumId, see Listing albums. If the albumId is invalid, a Bad Request error is returned. If the ID is valid, but the album doesn't exist for the authenticated user, a Not Found error is returned. For more details regarding error handling, see Performance tips and Best practices.

REST

Here is a sample request:

POST https://photoslibrary.googleapis.com/v1/mediaItems:search
Content-type: application/json
Authorization: Bearer oauth2-token
{
  "pageSize": "100",
  "albumId": "album-id"
}

The POST request returns the following response:

{
  "mediaItems": [
    ...
  ],
  "nextPageToken": "token-for-pagination"
}

Java

try {
  // Make a request to list all media items in an album
  // Provide the ID of the album as a parameter in the searchMediaItems call
  // Iterate over all the retrieved media items
  SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(albumId);

  for (MediaItem item : response.iterateAll()) {
    // ...
  }

} catch (ApiException e) {
  // Handle error
}

PHP

try {
    // Make a request to list all media items in an album
    // Provide the ID of the album as a parameter in the searchMediaItems call
    // Iterate over all the retrieved media items
    $response = $photosLibraryClient->searchMediaItems(['albumId' => $albumId]);
    foreach ($response->iterateAllElements() as $item) {
        // ...
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

The response contains a nextPageToken and the list of media items. Unlike when listing library contents, the media items are returned by their order in the album. For more details, see mediaItems and Pagination. The user can edit the order in the Google Photos interface.

If the albumId is set, you can't apply a filter when listing album contents. Doing so results in a Bad Request error.

Listing shared albums

You can retrieve a list of all albums that the user has shared or that have been shared with a user. This is similar to the Sharing tab in the Google Photos app.

Shared albums that the user has added to their Google Photos library are also returned in the listing albums call. Make the following call to list shared albums through the Library API:

REST

Here is a sample request:

GET https://photoslibrary.googleapis.com/v1/sharedAlbums

The request returns the following result:

{
  "sharedAlbums": [...]
  "nextPageToken": "token-for-pagination"
}

Java

try {
  // Make a request to list all albums that have been shared by the user
  // Iterate over all the albums in this list
  // Pagination is handled automatically
  ListSharedAlbumsPagedResponse response = photosLibraryClient.listSharedAlbums();

  for (Album album : response.iterateAll()) {
    // ..
  }

} catch (ApiException e) {
  // Handle error
}

PHP

try {
    // Make a request to list all albums that have been shared by the user
    // Iterate over all the albums in this list
    // Pagination is handled automatically
    $response = $photosLibraryClient->listSharedAlbums();
    foreach ($response->iterateAllElements() as $album) {
        // ...
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

A list of albums is included in sharedAlbums. For more information, see Listing albums. The response also contains a nextPageToken. For more information, see Pagination.

Albums that your app has created and shared include an additional shareInfo property. For more details, see Share media.

Listing app created albums

You can list albums that have been created by your app with excludeNonAppCreatedData set to true in the following calls:

REST

Here is a GET request to list all the albums from the user's Google Photos library created only by your app:

GET https://photoslibrary.googleapis.com/v1/albums?excludeNonAppCreatedData=true
Content-type: application/json
Authorization: Bearer oauth2-token

Here is a GET request to list all the shared albums from the user's Google Photos library created only by your app:

GET https://photoslibrary.googleapis.com/v1/sharedAlbums?excludeNonAppCreatedData=true
Content-type: application/json
Authorization: Bearer oauth2-token

Java

try {
  // Make a request to list all albums that have been created by your app
  boolean excludeNonAppCreatedData = true;

  // Iterate over all the albums in this list
  // Pagination is handled automatically
  ListAlbumsPagedResponse response = photosLibraryClient.listAlbums(excludeNonAppCreatedData);

  for (Album album : response.iterateAll()) {
    // ..
  }

} catch (ApiException e) {
  // Handle error
}

try {
  // Make a request to list all shared albums that have been created by your app
  boolean excludeNonAppCreatedData = true;

  // Iterate over all the albums in this list
  // Pagination is handled automatically
  ListSharedAlbumsPagedResponse response =
      photosLibraryClient.listSharedAlbums(excludeNonAppCreatedData);

  for (Album album : response.iterateAll()) {
    // ..
  }

} catch (ApiException e) {
  // Handle error
}

PHP

try {
    // Make a request to list all albums that have been created by your app
    $response = $photosLibraryClient->listAlbums(['excludeNonAppCreatedData' => true]);

    // Iterate over all the albums in this list
    // Pagination is handled automatically
    foreach ($response->iterateAllElements() as $album) {
        // ...
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

try {
    // Make a request to list all shared albums that have been created by your app
    $response = $photosLibraryClient->listSharedAlbums(['excludeNonAppCreatedData' => true]);

    // Iterate over all the albums in this list
    // Pagination is handled automatically
    foreach ($response->iterateAllElements() as $album) {
        // ...
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

Pagination for REST

To improve performance, methods that return a large number of results (such as list methods) may paginate the response. The maximum number of results in each page is given by the pageSize parameter.

For calls to mediaItems:search and mediaItems:list, the default page size is 25 items. We recommend this page size because it strikes a balance between the size of the response and the fill rate. The maximum page size for media item search and list requests is 100 items.

The default and recommended page size when listing albums is 20 albums, with a maximum of 50 albums.

When the number of available results is greater than the page size, the response includes a nextPageToken, which indicates to your application that there are more results to be fetched from the server.

Example

You must append the nextPageToken to subsequent requests in the parameter pageToken, as shown in the following example. Specify the pageToken together with other parameters required for the operation, either in the body of the request, or as a query parameter.

Request #1

{
  "pageSize": "5",
  "filters": { … }
}

Response #1

{
  "mediaItem": [ … ],
  "nextPageToken": "next-page-token"
}

Request #2

{
  "pageSize": "5",
  "filters": { … },
  "pageToken": "page-token"
}

Response #2

{
  "mediaItem": [ … ],
  "nextPageToken": "next-page-token"
}

Continue this pattern until there are no more nextPageToken objects.

The nextPageToken is only valid for the same request. If any parameters are changed, a previously used nextPageToken shouldn't be used in the same request.