在游戏中集成 PGS Recall API

本页介绍了如何在游戏中实现 Recall API。其中首先介绍了 设置游戏服务器和客户端以支持该 API,然后 如何存储和检索令牌。

游戏服务器设置

设置您的游戏服务器,以便对 Google 服务器进行 Recall API 调用。

设置您的 Play 游戏服务项目

(如果尚未完成)请按照设置 Google Play 游戏服务中的说明进行操作。

为游戏设置服务账号

按照创建服务账号中的说明操作。最后,您应该得到一个包含服务账号凭据的 JSON 文件。

下载适用于 Play 游戏服务的服务器端 Java 库

下载最新版 google-api-services-games 然后将其上传到您的服务器

准备用于 Recall API 调用的凭据

请参阅准备创建委托 API 致电 获取更多背景信息。

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.games.Games;
import com.google.api.services.games.GamesScopes;

// ...

GoogleCredential credential =
  GoogleCredential.fromStream(new FileInputStream("<credentials>.json"))
    .createScoped(Collections.singleton(GamesScopes.ANDROIDPUBLISHER));

Games gamesApi =
    new Games.Builder(httpTransport, JSON_FACTORY, credential).build();

游戏客户端设置

设置游戏客户端,以检索您的服务器用于与 Google 服务器进行通信的 Recall 会话 ID。

Java SDK

在客户端中设置 Java SDK。 并确保包含 com.google.android.gms:play-services-games-v2:19.0.0 和 在 Gradle 中为 com.google.android.gms:play-services-tasks:18.0.2 或更高版本 文件。

要使用正确的信息与 Google 的服务器进行通信,请向 Google 服务器发出 找回您要发送到游戏服务器的客户端 SDK 中的会话 ID:

Kotlin

PlayGames.getRecallClient(getActivity())
  .requestRecallAccess()
  .addOnSuccessListener { recallAccess -> val recallSessionId: String = recallAccess.getSessionId() }
  // Send the recallSessionId to your game server

Java

PlayGames.getRecallClient(getActivity())
  .requestRecallAccess()
  .addOnSuccessListener(
    recallAccess -> {
      String recallSessionId = recallAccess.getSessionId();
      // Send the recallSessionId to your game server
  });

在游戏服务器中使用 Recall API

配置服务器和客户端后,您可以将 recallSessionID 从游戏客户端发送到游戏服务器,并按照以下指南开始使用 Java API 来存储、检索或删除 Recall 令牌服务器端。

存储令牌

使用 LinkPersonaRequest 对象存储用户的角色和游戏令牌。 使用GoogleCredential致电 Google API。 为了遵循 1:1 基数 限制条件,因此可以 一次只能将一个角色与一个 PGS 玩家资料相关联,反之亦然。将 解决政策,以防此 PGS 玩家资料已与另一个玩家资料关联 角色。

您还可以选择为令牌设置 TTL,用于声明 该令牌是有效的,使用 Durations 对象。您可以选择使用 SetTtl() 进行设置(如下所示),它会将 从方法中指定的时间期限算起的失效日期,或 setExpireTime():可让您设置令牌的确切过期时间。

您必须对角色和游戏令牌进行加密,且它们不能包含个人身份信息。角色和令牌字符串不得超过 256 个字符,并且每个游戏最多可以为每位玩家存储 20 个令牌或角色。

在给定的时间只能为每位玩家的每个角色存储一个令牌。如果您尝试 存储另一个具有相同角色的令牌,系统会覆盖原始令牌 令牌。

import com.google.api.services.games.Games.Recall.LinkPersona;
import com.google.protobuf.util.Durations;

// ...

Games gamesApi =
    new Games.Builder(httpTransport, JSON_FACTORY, credential).build();

String recallSessionId = ... // recallSessionID from game client
String persona = ... // encrypted opaque string, stable for in-game account
String token = ... // encrypted opaque string encoding the progress line

LinkPersonaRequest linkPersonaRequest =
  LinkPersonaRequest.newBuilder()
    .setSessionId(recallSessionId)
    .setPersona(persona)
    .setToken(token)
    .setCardinalityConstraint(ONE_PERSONA_TO_ONE_PLAYER)
    .setConflictingLinksResolutionPolicy(CREATE_NEW_LINK)
    .setTtl(Durations.fromDays(7)) // Optionally set TTL for token
    .build();

LinkPersonaResponse linkPersonaResponse =
  gamesApi.recall().linkPersona(linkPersonaRequest).execute();

if (linkPersonaResponse.getState() == LINK_CREATED) {
  // success
}

检索令牌

如需检索 Recall 令牌,请从客户端获取 recallSessionId,然后 将其传递到 retrieveTokens API:

import com.google.api.services.games.Games.Recall.RetrieveTokens;

// ...

String recallSessionId = ... // recallSessionID from game client

RetrievePlayerTokensResponse retrievePlayerTokensResponse =
  gamesApi.recall().retrieveTokens(recallSessionId).execute();

for (RecallToken recallToken : retrievePlayerTokensResponse.getTokens()) {
  String token recallToken.getToken();
  // Same string as was written in LinkPersona call
  // decrypt and recover in-game account
}

删除 Recall 令牌

如果需要,您还可以通过以下调用删除 Recall 令牌:

import com.google.api.services.games.Games.Recall.UnlinkPersona;

// ...

String recallSessionId = ...
String persona = ...
String token = ...

Games gamesApi =
    new Games.Builder(httpTransport, JSON_FACTORY, credential).build();

UnlinkPersonaRequest unlinkPersonaRequest =
  UnlinkPersonaRequest.newBuilder()
    .setSessionId(recallSessionId)
    .setPersona(persona)
    // .setToken(token) - alternatively set token, but not both
    .build();

UnlinkPersonaResponse unlinkPersonaResponse =
  gamesApi.recall().unlinkPersona(unlinkPersonaRequest).execute();

// Confirm that the unlinking process completed successfully.
boolean unlinked = unlinkPersonaResponse.isUnlinked();

启用无配置文件模式

您可以启用有限的 Recall API 功能 对于没有 PGS 玩家资料的用户,请按以下步骤操作:

  1. 在 Play 开发者中为 PGS 游戏项目启用无资料找回功能 控制台。 选择标有
  2. 请参阅本部分后面部分中的附加条款
  3. 将以下元数据标记添加到您的应用程序中 manifest
<meta-data
  android:name="com.google.android.gms.games.PROFILELESS_RECALL_ENABLED"
  android:value="true" />

附加条款

除了遵守《Play 游戏服务服务条款》 服务,即表示您同意,如果您将 Recall API 用于 没有 PGS 玩家资料的用户 最终用户在没有 Play 游戏服务玩家资料的情况下与 Google 分享的数据; 在与 Google 分享此类数据之前,您必须向最终用户 适当的通知,说明 1) 您与 Google 分享数据以实现 Play 游戏账号关联功能;2) 是否提供用于管理各种设置 例如通过 Play 游戏设置分享的数据;以及 3) 对 此类数据的 Google 隐私权政策 政策,并获取合适的最终用户 同意在符合所有适用法律要求的情况下进行此类共享。