讓使用者能順暢登入遊戲,同時繼續使用自己的帳戶系統。您可以使用 Play 遊戲服務喚回 API 將遊戲內帳戶連結至 Google Play 遊戲服務帳戶,這樣當使用者在不同裝置上 (或在重新安裝遊戲後使用相同裝置) 遊玩遊戲時,您就能查詢已連結的遊戲內帳戶,並簡化登入流程。
如果您已整合 Android Recall API,這些 Recall API 應該會讓您感到熟悉。任何與 Play 遊戲服務喚回整合的伺服器端整合,都可以由電腦遊戲重複使用,因為 Android 和電腦的整合方式相同。
必要條件
完成 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:在登入時要求喚回存取權
當遊戲處理登入流程時 (例如新增遊戲內帳戶),請使用 GamesRecallClient::RequestRecallAccess()
要求喚回存取權。
這個呼叫會傳回工作階段 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
遊戲取得喚回工作階段 ID 並將其傳遞至後端遊戲伺服器後,請使用 Play 遊戲伺服器端 REST API:
- 使用
recall.retrieveTokens
查詢現有的已連結遊戲內帳戶 - 使用
recall.linkPersona
新增或更新已連結的遊戲內帳戶 - 使用
recall.unlinkPersona
刪除已連結的遊戲內帳戶
如需更詳細的伺服器端整合指南,請參閱說明如何在遊戲伺服器中使用 Recall API 的文檔。