使用者可以共用應用程式建立的相簿,並提供以下選項: 可以評論他們的媒體項目,或向相簿提供自己的媒體項目。
如要透過 Google Photos Library API 分享相簿,應用程式必須執行下列步驟:
,瞭解如何調查及移除這項存取權。必要的驗證範圍
為了分享內容,應用程式必須要求 photoslibrary.sharing
授權範圍。
分享相簿
分享相簿前,請考量下列幾點:
- 應用程式只能分享自己建立的相簿。其他應用程式建立的相簿 包含 Google 相簿在內,您的應用程式就無法共享。
- 應用程式透過 Library API 分享相簿時, 可分享的網址 因此產生了可供所有人存取的相簿。
- 如果相簿是透過 API 分享,相簿擁有者可以 關閉連結共用設定 或取消分享 Google 相簿應用程式中的相簿,可能會導致 避免應用程式加入新使用者
如何共享相簿:
- 跟著 使用者體驗指南和 事先取得使用者的明確同意,才能建立共享相簿。
- 建立相簿、
並記錄其
albumId。如果已經建立好相簿,可以 擷取其「albumId」 列出使用者的相簿。 - 使用以下應用程式呼叫
albums.share: 相關的albumId以及 偏好的共用選項 設定。 - 錄製
shareToken敬上 傳回的值分享權杖是共享相簿的 ID 在不同使用者帳戶之間都能使用。 - 另一位使用者現在可以透過您的應用程式進行驗證,然後
join、
離開,或
擷取詳細資料
存取相簿的
shareToken。
分享選項
在使用相簿共享相簿時,您可以設定下列選項:
sharedAlbumOptions敬上
參數。如果未明確設定選項,系統會使用預設值。
| 屬性 | 預設值 | 說明 |
|---|---|---|
isCollaborative |
false |
設定其他 Google 相簿使用者能否在內容中新增內容 共享相簿。 |
isCommentable |
false |
設定其他 Google 相簿使用者能否在 共享相簿。 |
要求範例
以下要求呼叫 albums.share 並提供選項,分享相簿。A 罩杯
shareInfo 資源
會在描述相簿內容的回應中
共用資源。
REST
以下是分享相簿的 POST 要求標頭:
POST https://photoslibrary.googleapis.com/v1/albums/album-id:share Content-type: application/json Authorization: Bearer oauth2-token
在要求主體中,指定共用選項。
{
"sharedAlbumOptions": {
"isCollaborative": "true",
"isCommentable": "true"
}
}這個要求會傳回下列回應:
{
"shareInfo": {
"sharedAlbumOptions": {
"isCollaborative": "true",
"isCommentable": "true"
},
"shareableUrl": "shareable-url",
"shareToken": "share-token",
"isJoinable": "true-if-users-can-join-album",
"isJoined": "true-if-user-is-joined-to-album",
"isOwned": "true-if-user-owns-album"
}
}Java
try { SharedAlbumOptions options = // Set the options for the album you want to share SharedAlbumOptions.newBuilder() .setIsCollaborative(true) .setIsCommentable(true) .build(); ShareAlbumResponse response = photosLibraryClient.shareAlbum(albumId, options); // The response contains the shareInfo object, a url, and a token for sharing ShareInfo info = response.getShareInfo(); // Link to the shared album String url = info.getShareableUrl(); String shareToken = info // The share token which other users of your app can use to join the album you shared .getShareToken(); SharedAlbumOptions sharedOptions = info // The options set when sharing this album .getSharedAlbumOptions(); } catch (ApiException e) { // Handle error }
PHP
// Set the options for the album you want to share $options = new SharedAlbumOptions(); $options->setIsCollaborative(true); $options->setIsCommentable(true); try { $response = $photosLibraryClient->shareAlbum($albumId, ['sharedAlbumOptions' => $options]); // The response contains the shareInfo object, a url, and a token for sharing $shareInfo = $response->getShareInfo(); // Link to the shared album $url = $shareInfo->getShareableUrl(); // The share token which other users of your app can use to join the album you shared $shareToken = $shareInfo->getShareToken(); // The options set when sharing this album $sharedOptions = $shareInfo->getSharedAlbumOptions(); } catch (\Google\ApiCore\ApiException $e) { // Handle error }
共享相簿屬性
對於應用程式建立並分享的相簿,
傳回 album
加入其他資源
shareInfo。這個
資源會在分享時傳回。
列出
擷取相簿。
下表列出
shareInfo敬上
屬性:
| 屬性 | |
|---|---|
sharedAlbumOptions |
用來說明使用者權限的選項 新增媒體 共享相簿的項目或評論 |
shareableUrl |
共享 Google 相簿的相簿連結。知道連結的使用者皆可 查看相簿內容,因此請謹慎處理。 只有在相簿時,才會傳回 如果擁有者關閉連結,「 |
shareToken |
權杖用於 join、 離開、 或擷取 管理員以外的使用者所代表共享相簿的詳細資料 擁有者。 如果擁有者關閉連結,「 |
isJoinable |
True (如果使用者可以加入相簿)。 |
isJoined |
True:如果使用者加入相簿。一律採取這種做法
true。 |
isOwned |
如果使用者是相簿的擁有者,則為 True。 |
取消共享相簿
如要取消分享應用程式先前分享的相簿,請撥打
albums.unshare 使用
專輯的 albumId。
除了停止共享相簿外,下列項目還會受到以下影響: 發生:
- 所有非擁有者都將無法存取相簿。這包括擁有 CANNOT TRANSLATE 具體分享 分享自己的相片。
- 凡是由非擁有者新增的內容,都將從相簿中移除。
- 如果使用者先前已將專輯內容加入媒體庫,則 內容會保留在他們的媒體庫中。
- 相簿的分享權杖和可分享網址將失效。
要求範例
REST
以下是取消分享相簿的 POST 要求標頭:
POST https://photoslibrary.googleapis.com/v1/albums/album-id:unshare Content-type: application/json Authorization: Bearer oauth2-token
要求主體必須為空白。
如果要求成功,會傳回包含 HTTP 的空白回應 成功的狀態碼如果要求失敗,則會傳回 HTTP 傳回錯誤訊息。
Java
try { // If this call is not successful, an exception is raised photosLibraryClient.unshareAlbum(albumId); } catch (ApiException e) { // Handle error }
PHP
try { // Mark the album as private and no longer shared // If this call is not successful, an exception is raised $photosLibraryClient->unshareAlbum($albumId); } catch (\Google\ApiCore\ApiException $e) { // Handle error }
擷取共享相簿並分享權杖
共享相簿的詳細資料 當應用程式分享相簿時,會傳回包含其分享權杖。你可以 之後也能以下列方式擷取這些詳細資料
如果目前連結到應用程式的使用者是擁有者,或已加入 專輯:
- 使用
albums.get時,透過 相關albumId。 - 採用
albums.list(如有) 是指相簿中的媒體項目。 - 取代為
sharedAlbums.list、 這會傳回使用者加入或擁有的所有共享相簿。擷取 僅限應用程式建立的相簿,請使用excludeNonAppCreatedData敬上 參數。 - 通話對象:
sharedAlbums.get共用權杖
如果目前連結到應用程式的使用者尚未加入相簿,您可以
擷取共享相簿的詳細資料
sharedAlbums.get,使用
有效的共用權杖
要求範例
REST
以下是由shareToken取得專輯的要求:
GET https://photoslibrary.googleapis.com/v1/sharedAlbums/share-token
如果要求成功,則會傳回共用的
album
詳細資料。
Java
try { // Get a shared album from its share token Album sharedAlbum = photosLibraryClient.getSharedAlbum(shareToken); String id = sharedAlbum.getId(); String title = sharedAlbum.getTitle(); // ... } catch (ApiException e) { // Handle error }
PHP
try { // Get the album from a share token $album = $photosLibraryClient->getSharedAlbum($shareToken); // Get some properties of an album $productUrl = $album->getProductUrl(); $title = $album->getTitle(); } catch (\Google\ApiCore\ApiException $e) { // Handle error }
加入共享相簿
應用程式可以代表使用者加入共享相簿,並設定共享相簿 產生下一個符記但前提是必須符合下列條件:
- 您的應用程式有 建立並共用 專輯
- 你要加入相簿的使用者不是相簿擁有者。也就是說,
相簿的
isOwned欄位shareInfo會傳回 false。 - 共用權杖有效。
- 相簿
shareInfo中的isJoinable欄位為 true。
REST
以下是加入共享相簿的 POST 要求標頭:
POST https://photoslibrary.googleapis.com/v1/sharedAlbums:join Content-type: application/json Authorization: Bearer oauth2-token
在要求主體中指定 shareToken。
{
"shareToken": "share-token"
}POST 要求會傳回共用的 album
代表使用者加入您的應用程式
Java
try { // Join the shared album using the share token obtained when sharing the album // If this call is not successful, an exception is raised JoinSharedAlbumResponse response = photosLibraryClient.joinSharedAlbum(shareToken); Album joinedAlbum = response.getAlbum(); } catch (ApiException e) { // Handle error }
PHP
try { $response = $photosLibraryClient->joinSharedAlbum($shareToken); // Join the shared album using the share token obtained when sharing the album // If this call is not successful, an exception is raised $joinedAlbum = $response->getAlbum(); } catch (\Google\ApiCore\ApiException $e) { // Handle error }
離開共享相簿
應用程式可以透過相簿的分享功能,代表使用者離開共享相簿 產生下一個符記但前提是必須符合下列條件:
- 應用程式已建立並分享該相簿。
- 使用者目前已加入相簿。也就是說,
isJoined欄位 相簿中的shareInfo為 true。 - 連結至應用程式的使用者並非相簿擁有者。也就是說,
相簿
shareInfo中的isOwned欄位為 false。
REST
以下是離開共享相簿的 POST 要求標頭:
POST https://photoslibrary.googleapis.com/v1/sharedAlbums:leave Content-type: application/json Authorization: Bearer oauth2-token
在要求主體中指定 shareToken。
{
"shareToken": "share-token"
}如果要求成功,會傳回包含 HTTP 的空白回應 成功的狀態碼如果要求失敗,則會傳回 HTTP 傳回錯誤訊息。
Java
try { // Leave a shared album using its share token // If this call is not successful, an exception is raised photosLibraryClient.leaveSharedAlbum(shareToken); } catch (ApiException e) { // Handle error }
PHP
try { // Leave the shared album using the share token obtained when sharing the album // If this call is not successful, an exception is raised $photosLibraryClient->leaveSharedAlbum($shareToken); } catch (\Google\ApiCore\ApiException $e) { // Handle error }
共用媒體項目屬性
所屬的媒體項目
應用程式分享的相簿包含其他資源
contributorInfo。
這個屬性只有在列出
共享相簿的內容
contributorInfo 屬性包含新增媒體的使用者名稱
新增至相簿,而
個人資料的 base URL
圖片。
範例如下:
{
"id: "media-item-id",
...,
"mediaMetadata": {
...
}
"contributorInfo": {
"profilePictureBaseUrl": "profile-picture-base-url_use-only-with-parameters",
"displayName": "name-of-user"
}
}