在继续使用您自己的账号系统的同时,让用户能够顺畅地登录您的游戏。借助 Play Games Services Recall API,您可以将游戏内账号与 Google Play Games 服务账号相关联,然后当用户在不同设备上玩您的游戏(或在重新安装游戏后在同一设备上玩游戏)时,您可以查询关联的游戏内账号并简化登录流程。
如果您已与 Android Recall API 集成,那么这些 Recall API 应该很熟悉。任何与 Play 游戏服务 Recall 的服务器端集成都可以供 PC 版游戏重复使用,因为它们在 Android 和 PC 上是相同的。
前提条件
完成 SDK 设置。
阅读 Play Games Services Recall API 的概览。
在 Play 管理中心内完成 Google Play 游戏服务设置。
第 1 步:在清单中添加 Play Games 服务项目 ID
在 Play 管理中心内完成 Play 游戏服务设置后,您的游戏现在已关联 Play 游戏服务的项目 ID。使用此项目 ID(可在 Play 管理中心的 Play 游戏服务的配置页面中找到),更新游戏的 manifest.xml。
示例 manifest.xml 内容:
<?xml version="1.0" encoding="utf-8"?>
<Manifest version="1">
<Application>
<PackageName>com.example.package</PackageName>
<PlayGamesServices>
<ProjectId>123456789</ProjectId>
</PlayGamesServices>
</Application>
</Manifest>
第 2 步:在登录时请求 Recall 访问权限
当游戏处理登录流程时(例如添加游戏内账号),请使用 GamesRecallClient::RequestRecallAccess() 请求 Recall 访问权限。
此调用会返回一个会话 ID,您的后端会使用该 ID 向 Google 发出服务器端调用,以将游戏内账号与 Play 游戏服务用户相关联和取消关联。
auto promise = std::make_shared<std::promise<RecallAccessResult>>();
games_recall_client.RequestRecallAccess(params, [promise](RecallAccessResult result) {
promise->set_value(std::move(result));
});
auto recall_access_result = promise->get_future().get();
if (recall_access_result.ok()) {
auto recall_session_id = recall_access_result.value().recall_session_id;
// Pass the recall session ID to your backend game server so it can query
// for an existing linked in-game account.
// - If you discover an existing linked in-game account, continue to sign-in
// the in-game account. This provides a seamless cross-device sign-in
// experience.
// - If there is not an existing linked in-game account, when the user
// completes the sign-in using your in-game account system record the
// account linking with Play Games Services Recall. This helps to provide
// a seamless cross-device sign-in experience when the user returns on a
// different device or after re-installing your game on the same device.
} else {
// Handle the error
}
第 3 步:处理 Recall 会话 ID
游戏获得 Recall 会话 ID 并将其传递给后端游戏服务器后,您可以使用 Play Games 服务器端 REST API 来执行以下操作:
- 使用
recall.retrieveTokens查询现有的关联游戏内账号 - 使用
recall.linkPersona添加或更新关联的游戏内账号 - 使用
recall.unlinkPersona删除关联的游戏内账号
如需更详细的服务器端集成指南,请参阅有关如何在游戏服务器中使用 Recall API 的文档。