让用户能够顺畅地登录您的游戏,同时继续使用自己的账号系统。借助 Play 游戏服务 Recall API,您可以将游戏内账号与 Google Play 游戏服务账号相关联,这样,当用户在不同设备(或在重新安装游戏后在同一设备上)玩您的游戏时,您可以查询关联的游戏内账号并简化登录流程。
如果您已与 Android Recall API 集成,那么这些 Recall API 应该看起来很熟悉。与 Play 游戏服务 Recall 进行的任何服务器端集成都可以供 PC 版游戏重复使用,因为它们在 Android 和 PC 上是相同的。
前提条件
完成 SDK 设置。
阅读 Play 游戏服务 Recall API 概览。
在 Play 管理中心内完成 Google Play 游戏服务设置。
第 1 步:在清单中添加 Play 游戏服务项目 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 游戏服务器端 REST API 执行以下操作:
- 使用
recall.retrieveTokens查询现有关联的游戏内账号 - 使用
recall.linkPersona添加或更新关联的游戏内账号 - 使用
recall.unlinkPersona删除关联的游戏内账号
如需更详细地了解服务器端集成,请参阅 有关如何在游戏服务器中使用 Recall API 的 文档。