您的应用创建的影集可以在用户之间共享,并提供相关选项 在影集中发表评论,或向影集上传自己的媒体内容。
如需通过 Google Photos Library API 分享影集,您的应用需要执行以下操作:
- 代表用户创建影集。
- 将该影集设为“已分享”。
必需的身份验证范围
如要分享内容,您的应用必须请求 photoslibrary.sharing
授权范围。
分享影集
在分享影集之前,请考虑以下事项:
- 您的应用只能分享自己创建的影集。由其他应用创建的影集、 (包括 Google 相册)无法由您的应用分享。
- 当您的应用通过 Library API 分享影集时, 可分享的网址 任何人都可以用来访问影集。
- 对于通过 API 分享的影集,影集所有者可以 关闭链接共享 或在 Google 相册应用中停止分享该影集 将新用户加入应用
要分享影集,请执行以下操作:
- 按照 用户体验指南和 征得用户的明确同意,才能创建共享影集。
- 创建影集,
并记录其
albumId
。如果您已创建影集,则可以 检索其albumId
, 列出用户的影集。 - 使用以下方式调用
albums.share
: 相关的albumId
,以及 分享选项 。 - 录制
shareToken
值。共享令牌是共享影集的标识符 可以跨不同的用户账号使用 - 另一个用户现在可以使用您的应用进行身份验证
join、
leave 或
检索详细信息
使用
shareToken
分享相册内容。
分享选项
使用
sharedAlbumOptions
参数。如果未明确设置选项,系统将使用默认值。
属性 | 默认值 | 说明 |
---|---|---|
isCollaborative |
false |
设置其他 Google 相册用户能否向 共享影集。 |
isCommentable |
false |
设置其他 Google 相册用户能否对 共享影集。 |
示例请求
以下请求通过使用选项调用 albums.share
来分享影集。答
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
。这个
那么 sharing 时将返回该属性值,
商家信息或
检索影集。
下表列出了
shareInfo
属性:
属性 | |
---|---|
sharedAlbumOptions |
说明用户是否可以 添加媒体 在共享影集中添加内容或评论内容。 |
shareableUrl |
指向 Google 相册中的共享影集的链接。知道链接的任何人都可以 可查看影集的内容,因此应谨慎处理。 仅当影集符合以下所有条件时,系统才会返回 如果所有者关闭链接, |
shareToken |
一个令牌,用于 join、 leave、 或检索 您可以代表其他用户(该用户不是管理员)分享影集的详细信息 所有者。 如果所有者关闭链接, |
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
如果请求成功,
<ph type="x-smartling-placeholder"></ph>
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
属性包含添加媒体的用户的名称
将内容添加到影集,而
个人资料的基准网址
图片。
示例如下:
{ "id: "media-item-id", ..., "mediaMetadata": { ... } "contributorInfo": { "profilePictureBaseUrl": "profile-picture-base-url_use-only-with-parameters", "displayName": "name-of-user" } }