The Ambient API enables your application to connect ambient devices to a user's Google Photos account and display their selected photos.
The Ambient API Flow
Here's a breakdown of how the Ambient API works to connect a device and then retrieve and display media items:
Check for existing device (recommended): Before creating a new device, it's recommended to check if a device for the current user already exists. Your application should maintain a mapping between your internal user and the Google-provided
deviceIdfor any devices they create through your app. If adeviceIdis found for the user, you can proceed to refreshing their authorization token (if needed).Initiate OAuth 2.0 authorization (and optionally create device): Begin the OAuth 2.0 for TV and Limited Input Devices flow by requesting an authorization code.
Create a new device: Your app creates a device in a user's Google Photos account by calling
CreateDeviceand providing a valid v4 UUID.Upon successful device creation, the API will return an
AmbientDeviceobject containing a Google-assigneddeviceId. It is crucial for your application to store thisdeviceIdand associated it with your users.Display the
settingsUri: anAmbientDeviceobject includes asettingsUri. Present this URI to the user, typically as a QR code, which the user can scan using their mobile device. This URI directs the user to the Google Photos app where they can configure the media sources (e.g., albums) they want to display on their ambient device.Poll for
mediaSourcesSet: Your application should periodically call theGetDevicemethod, providing thedeviceId, to check the status of the ambient device. Monitor themediaSourcesSetfield in theAmbientDeviceresponse. It will initially be false.Once the user has successfully selected media sources in the Google Photos app, this field will change to true.
The
AmbientDeviceresponse includes apollingConfigwith apollIntervalthat you should use as a guideline for your polling frequency.Retrieve media items: When
mediaSourcesSetreturns true, your application can begin fetching the media items selected by the user.Call the
ListMediaItemsmethod, providing thedeviceId. The API will return aListMediaItemsResponsecontaining a list ofAmbientMediaItemobjects. EachAmbientMediaItemincludes details like anid,createTime, and aMediaFileobject with additional metadata. TheMediaFilecontains abaseUrlwhich you can use to fetch the actual bytes of a media item. Review the guide to List and retrieve media items for details on additionalbaseUrlparameters.Display Media Items: Use the
baseUrlfrom theMediaFileto download and display the media content on the ambient device.
Important Considerations
Device limit and management:
- Device limits: Be aware of the limit of 100 devices per user of your application.
- Device activity and tokens: You'll need to manage the lifecycle of devices and user authorization tokens. Consider how long devices remain active and how you'll handle token refreshes or re-authorization if a device becomes inactive or the token expires.
The guide to Create and manage devices has additional details.
Working with media items:
- Media item usage: Understand how to properly fetch and handle the media item
content using the
baseUrl, including any necessary authentication or parameters. - Error handling: Implement robust error handling for API calls, including
scenarios like
NOT_FOUNDfor devices,FAILED_PRECONDITIONif media sources are not set, andRESOURCE_EXHAUSTEDif device limits are reached.
The guide to List and retrieve media items has additional details.
Next Steps
- Configure your application: Verify you have the necessary credentials and have configured your application for OAuth 2.0 for TV and Limited Input Devices.
- Review the Ambient API Reference Documentation: Explore the detailed reference documentation for all available methods, request and response parameters, and error codes.