After making calls to list the contents of a photo library or album, instead of storing the returned media items, your application should store the IDs of the media items. This is because the content of the media items may change and after a certain time, the URLs included in the response expire. The media item ID uniquely identifies a media item, such as a photo or a video inside a user’s library.
Note that your application shouldn't cache a user's photo or video for long periods of time, but depending on your use case, you may store or cache the media item ID for as long as necessary. You should also note that access to user data is governed by privacy obligations.
Required authentication scope
To access media items, your app must request at least one of the following authorization scopes. Access to the media items returned in the response depends on the scopes you have requested.
.readonly
allows access to all media items in the user's library.readonly.appcreateddata
allows access only to media items that were created by the app
Media items
A
mediaItem
is a representation of media such as a photo or a video that’s been
uploaded to the Google Photos library. It is a top-level object and its properties can differ
based on the underlying media type.
The following table lists the mediaItem
properties:
Properties | |
---|---|
id
| A permanent, stable ID used to identify the object. |
description
| Description of the media item as seen inside Google Photos. |
baseUrl
| Used to access the raw bytes. For more information, see Base URLs. |
productUrl
| A link to the image inside Google Photos. This link can't be opened by the developer, only by the user. |
mimeType
| The type of the media item to help easily identify the type of media (for example:
image/jpg ). |
filename
| The filename of the media item shown to the user in the Google Photos app (within the item's info section). |
mediaMetadata
| Varies depending on the underlying type of the media, such as,
photo or
video . To reduce
the payload, field masks can be used. |
contributorInfo
| This field is only populated if the media item is in a shared album created by this app and
the user has granted the Contains information about the contributor who added this media item. For more details, see Share media. |
Get a media item
To retrieve a media item, call
mediaItems.get using the
mediaItemId
. The request returns a single media item.
The mediaItem
contains properties, such as the ID, description, URL,
contributor information. The additional information within photo
or
video
is based on the meta data within the file. ContributorInfo
contains metadata for items that are part of a shared album. Not all properties may be present.
If the media item is a video, the video file must be processed first. The mediaItem
contains a status
field inside mediaMetadata
which describes the
processing state of the video file. A newly uploaded file returns the
videoProcessingStatus
with the value PROCESSING
first, before it
is READY
for use. The baseUrl
of a video media item isn't available
until the video has been processed.
REST
Here is a GET request:
GET https://photoslibrary.googleapis.com/v1/mediaItems/MEDIA_ITEM_ID Content-type: application/json Authorization: Bearer OAUTH2_TOKEN
The response for a photo media item looks like this. The photo property contains metadata for photo items.
{ "id": "MEDIA_ITEM_ID", "description": "ITEM_DESCRIPTION", "productUrl": "URL_TO_OPEN_IN_GOOGLE_PHOTOS", "baseUrl": "BASE_URL-DO_NOT_USE_DIRECTLY", "mimeType": "MIME_TYPE_OF_MEDIA", "filename": "ITEM_FILENAME", "mediaMetadata": { "width": "MEDIA_ITEM_WIDTH", "height": "MEDIA_ITEM_HEIGHT", "creationTime": "MEDIA_ITEM_CREATION_TIME", "photo": { "cameraMake": "MAKE_OF_THE_CAMERA", "cameraModel": "MODEL_OF_THE_CAMERA", "focalLength": "FOCAL_LENGTH_OF_THE_CAMERA_LENS", "apertureFNumber": "APERTURE_F_NUMBER_OF_THE_CAMERA_LENS", "isoEquivalent": "ISO_OF_THE_CAMERA", "exposureTime": "EXPOSURE_TIME_OF_THE_CAMERA_APERTURE" } }, "contributorInfo": { "profilePictureBaseUrl": "PROFILE_PICTURE_BASE_URL-DO_NOT_USE_DIRECTLY", "displayName": "NAME_OF_USER" } }
The response for a video media item looks like this. The video property contains metadata for video items.
{ "id": "MEDIA_ITEM_ID", "description": "ITEM_DESCRIPTION", "productUrl": "URL_TO_OPEN_IN_GOOGLE_PHOTOS", "baseUrl": "BASE_URL-DO_NOT_USE_DIRECTLY", "mimeType": "MIME_TYPE_OF_MEDIA", "filename": "ITEM_FILENAME", "mediaMetadata": { "width": "MEDIA_ITEM_WIDTH", "height": "MEDIA_ITEM_HEIGHT", "creationTime": "MEDIA_ITEM_CREATION_TIME", "video": { "cameraMake": "MAKE_OF_THE_CAMERA", "cameraModel": "MODEL_OF_THE_CAMERA", "fps": "FRAME_RATE_OF_THE_VIDEO", "status": "READY" }, }, "contributorInfo": { "profilePictureBaseUrl": "PROFILE_PICTURE_BASE_URL-DO_NOT_USE_DIRECTLY", "displayName": "NAME_OF_USER" } }
Java
The photo property contains metadata for photo items.
try { // Get a media item using its ID String mediaItemId = "..."; MediaItem item = photosLibraryClient.getMediaItem(mediaItemId); // Get some properties from the retrieved media item String id = item.getId(); String description = item.getDescription(); String baseUrl = item.getBaseUrl(); String productUrl = item.getProductUrl(); // ... if (item.hasMediaMetadata()) { // The media item contains additional metadata, such as the height and width MediaMetadata metadata = item.getMediaMetadata(); long height = metadata.getHeight(); long width = metadata.getWidth(); Timestamp creationTime = metadata.getCreationTime(); // ... if (metadata.hasPhoto()) { // This media item is a photo and has additional photo metadata Photo photoMetadata = metadata.getPhoto(); String cameraMake = photoMetadata.getCameraMake(); String cameraModel = photoMetadata.getCameraModel(); float aperture = photoMetadata.getApertureFNumber(); int isoEquivalent = photoMetadata.getIsoEquivalent(); // ... } } if (item.hasContributorInfo()) { // A user has contributed this media item to a shared album ContributorInfo contributorInfo = item.getContributorInfo(); String profilePictureBaseUrl = contributorInfo.getProfilePictureBaseUrl(); String displayName = contributorInfo.getDisplayName(); } } catch (ApiException e) { // Handle error }
The video property contains metadata for video items.
try { // Get a media item using its ID String mediaItemId = "..."; MediaItem item = photosLibraryClient.getMediaItem(mediaItemId); // Get some properties from the retrieved media item String id = item.getId(); String description = item.getDescription(); String baseUrl = item.getBaseUrl(); String productUrl = item.getProductUrl(); // ... if (item.hasMediaMetadata()) { // The media item contains additional metadata, such as the height and width MediaMetadata metadata = item.getMediaMetadata(); long height = metadata.getHeight(); long width = metadata.getWidth(); Timestamp creationTime = metadata.getCreationTime(); // ... if (metadata.hasVideo()) { // This media item is a video and has additional video metadata Video videoMetadata = metadata.getVideo(); VideoProcessingStatus status = videoMetadata.getStatus(); if (status.equals(VideoProcessingStatus.READY)) { // This video media item has been processed String cameraMake = videoMetadata.getCameraMake(); String cameraModel = videoMetadata.getCameraModel(); double fps = videoMetadata.getFps(); // ... } } } if (item.hasContributorInfo()) { // A user has contributed this media item to a shared album ContributorInfo contributorInfo = item.getContributorInfo(); String profilePictureBaseUrl = contributorInfo.getProfilePictureBaseUrl(); String displayName = contributorInfo.getDisplayName(); } } catch (ApiException e) { // Handle error }
PHP
The photo property contains metadata for photo items.
try { // Get a media item using its ID $mediaItemId = "..."; $item = $photosLibraryClient->getMediaItem($mediaItemId); // Get some properties from the retrieved media item $id = $item->getId(); $description = $item->getDescription(); $baseUrl = $item->getBaseUrl(); $productUrl = $item->getProductUrl(); // ... $metadata = $item->getMediaMetadata(); if (!is_null($metadata)) { // The media item contains additional metadata, such as the height and width $height = $metadata->getHeight(); $width = $metadata->getWidth(); $creationTime = $metadata->getCreationTime(); // ... $photoMetadata = $metadata->getPhoto(); if (!is_null($photoMetadata)) { // This media item is a photo and has additional photo metadata $cameraMake = $photoMetadata->getCameraMake(); $cameraModel = $photoMetadata->getCameraModel(); $aperture = $photoMetadata->getApertureFNumber(); $isoEquivalent = $photoMetadata->getIsoEquivalent(); // ... } } $contributorInfo = $item->getContributorInfo(); if (!is_null($contributorInfo)) { // A user has contributed this media item to a shared album $profilePictureBaseUrl = $contributorInfo->getProfilePictureBaseUrl(); $displayName = $contributorInfo->getDisplayName(); } } catch (\Google\ApiCore\ApiException $e) { // Handle error }
The video property contains metadata for video items.
try { // Get a media item using its ID $mediaItemId = "..."; $item = $photosLibraryClient->getMediaItem($mediaItemId); // Get some properties from the retrieved media item $id = $item->getId(); $description = $item->getDescription(); $baseUrl = $item->getBaseUrl(); $productUrl = $item->getProductUrl(); // ... $metadata = $item->getMediaMetadata(); if (!is_null($metadata)) { // The media item contains additional metadata, such as the height and width $height = $metadata->getHeight(); $width = $metadata->getWidth(); $creationTime = $metadata->getCreationTime(); // ... $videoMetadata = $metadata->getVideo(); if (!is_null($videoMetadata)) { // This media item is a video and has additional video metadata if (VideoProcessingStatus::READY == $videoMetadata->getStatus()) { // This video media item has been processed $cameraMake = $videoMetadata->getCameraMake(); $cameraModel = $videoMetadata->getCameraModel(); $fps = $videoMetadata->getFps(); // ... } } } $contributorInfo = $item->getContributorInfo(); if (!is_null($contributorInfo)) { // A user has contributed this media item to a shared album $profilePictureBaseUrl = $contributorInfo->getProfilePictureBaseUrl(); $displayName = $contributorInfo->getDisplayName(); } } catch (\Google\ApiCore\ApiException $e) { // Handle error }
Get multiple media items
To retrieve multiple media items by their identifiers, call
mediaItems.batchGet
using the mediaItemId
s.
The request returns a list of
MediaItemResults
in the order of the media items identifiers supplied in the
request. Each result contains a
MediaItem
or a
Status
if there was an error:
The maximum number of media items you can request in one call is 50. The list of media items must not contain duplicate identifiers and cannot be empty.
REST
Here is a GET request that shows successful and unsuccessful access of media items. Specify
each media item identifier as a new mediaItemIds
query parameter as part of the
request:
GET https://photoslibrary.googleapis.com/v1/mediaItems:batchGet?mediaItemIds=MEDIA_ITEM_ID&mediaItemIds=ANOTHER_MEDIA_ITEM_ID&mediaItemIds=INCORRECT_MEDIA_ITEM_ID Content-type: application/json Authorization: Bearer OAUTH2_TOKEN
The GET request returns the following response:
{ "mediaItemResults": [ { "mediaItem": { "id": "MEDIA_ITEM_ID", ... } }, { "mediaItem": { "id": "ANOTHER_MEDIA_ITEM_ID", ... } }, { "status": { "code": 3, "message": "Invalid media item ID." } } ] }
Java
try { // List of media item IDs to retrieve List<String> mediaItemIds = Arrays .asList("MEDIA_ITEM_ID", "ANOTHER_MEDIA_ITEM_ID", "INCORRECT_MEDIA_ITEM_ID"); // Get a list of media items using their IDs BatchGetMediaItemsResponse response = photosLibraryClient .batchGetMediaItems(mediaItemIds); // Loop over each result for (MediaItemResult result : response.getMediaItemResultsList()) { // Each MediaItemresult contains a status and a media item if (result.hasMediaItem()) { // The media item was successfully retrieved, get some properties MediaItem item = result.getMediaItem(); String id = item.getId(); // ... } else { // If the media item is not set, an error occurred and the item could not be loaded // Check the status and handle the error Status status = result.getStatus(); // ... } } } catch (ApiException e) { // Handle error }
PHP
try { // List of media item IDs to retrieve $mediaItemIds = ["MEDIA_ITEM_ID", "ANOTHER_MEDIA_ITEM_ID", "INCORRECT_MEDIA_ITEM_ID"]; // Get a list of media items using their IDs $response = $photosLibraryClient->batchGetMediaItems($mediaItemIds); // Loop over each result foreach ($response->getMediaItemResults() as $itemResult) { // Each MediaItemresult contains a status and a media item $mediaItem = $itemResult->getMediaItem(); if(!is_null($mediaItem)){ // The media item was successfully retrieved, get some properties $id = $mediaItem->getId(); // ... } else { // If the media item is null, an error occurred and the item could not be loaded } } } catch (\Google\ApiCore\ApiException $e) { // Handle error }
Base URLs
Base URLs within the Google Photos Library API allow you to access the bytes of the media items. Using the various base URLs, your app can either download the media items or display the media items within your app. Base URLs are strings which are included in the response when you list albums or access media items. They are valid for 60 minutes and require additional parameters as they cannot be used as is.
The various base URLs are:
baseUrl
: Directly access photo, thumbnail for a video or download video bytes.coverPhotoBaseUrl
: Directly access the cover photo for the album.profilePictureBaseUrl
: Directly access the profile photo of the owner of amediaItem
.
Image base URLs
Here is the list of options you can use with the image base URLs:
Parameter | |
---|---|
w , h |
Description The width, To access an image media item, such as a photo or a thumbnail for a video, you must specify the dimensions that you plan to display in your application (so that the image can be scaled into these dimensions while preserving the aspect ratio). To do this, concatenate the base URL with your required dimensions as shown in the examples. Examples: BASE_URL=wMAX_WIDTH-hMAX_HEIGHT Here is an example to display a media item no wider than 2048 px and no taller than 1024 px: https://lh3.googleusercontent.com/p/AF....VnnY=w2048-h1024 |
c |
Description The crop, If you want to crop the image to the exact width and height dimensions you have specified,
concatenate the base URL with the optional The size (in pixels) should be in the range [1, 16383]. If either the width or the height of the image, exceeds the requested size, the image is scaled down and cropped (maintaining the aspect ratio). Examples: BASE_URL=wMAX_WIDTH-hMAX_HEIGHT-c In this example, the application displays a media item that is exactly 256 px wide by 256 px high, such as a thumbnail: https://lh3.googleusercontent.com/p/AF....VnnY=w256-h256-c |
d |
Description The download, If you want to download the image retaining all the EXIF metadata except the location
metadata, concatenate the base URL with the Examples: BASE_URL=d In this example, the application downloads an image with all metadata except the location metadata: https://lh3.googleusercontent.com/p/Az....XabC=d |
Video base URLs
Here is the list of options you can use with the video base URLs:
Parameter | |
---|---|
dv |
Description To access the bytes of a video Here is the format: BASE_URL=dv Examples: The following example shows you how to download the bytes of a video: https://lh3.googleusercontent.com/p/AF....BsdZ=dv |
w , h , c , and d |
Description To access the thumbnail of the video use any of the image base url parameters. Examples: Refer the table for the examples. |