ผสานรวม PGS Recall API ภายในเกม

หน้านี้จะอธิบายวิธีใช้ Recall API ใน เกมของคุณ ขั้นแรกจะครอบคลุมการตั้งค่าเซิร์ฟเวอร์เกมและไคลเอ็นต์ของคุณเพื่อรองรับ API ต่อจากนั้นจึงระบุวิธีจัดเก็บและเรียกข้อมูลโทเค็น

การตั้งค่าเซิร์ฟเวอร์เกม

ตั้งค่าเซิร์ฟเวอร์เกมของคุณให้เรียก Recall API ไปยังเซิร์ฟเวอร์ของ Google

1. ตั้งค่าโปรเจ็กต์บริการเกมของ Play

(หากยังไม่ได้ทำ) ทำตามคำแนะนำในการตั้งค่า Google Play บริการเกม

2. ตั้งค่าบัญชีบริการสำหรับเกม

ทำตามคำแนะนำในการสร้างบริการ บัญชี และส่วนท้ายควรมีไฟล์ JSON ที่มีข้อมูลเข้าสู่ระบบของบัญชีบริการ

3. ดาวน์โหลดไลบรารี Java ฝั่งเซิร์ฟเวอร์สำหรับ PlayGamesServices

ดาวน์โหลด google-api-services-games เวอร์ชันล่าสุด คลัง และอัปโหลดไปยังเซิร์ฟเวอร์ของคุณ

4. เตรียมข้อมูลเข้าสู่ระบบสำหรับการเรียก 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

Java SDK

ตั้งค่า Java SDK ภายในไคลเอ็นต์ และ ตรวจสอบว่าได้ใส่ com.google.android.gms:play-services-games-v2:19.0.0 แล้ว และ com.google.android.gms:play-services-tasks:18.0.2 ขึ้นไปใน Gradle

ในการสื่อสารกับเซิร์ฟเวอร์ของ Google ด้วยข้อมูลที่ถูกต้อง คุณต้อง ขอรหัสเซสชัน Recall จาก SDK ของไคลเอ็นต์ซึ่งคุณส่งไปยังเกมของคุณ เซิร์ฟเวอร์

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
    });

SDK สำหรับ Unity

ตั้งค่า Unity SDK ภายใน

ในการสื่อสารกับเซิร์ฟเวอร์ของ Google ด้วยข้อมูลที่ถูกต้อง คุณต้อง ขอรหัสเซสชัน Recall จาก SDK ของไคลเอ็นต์ และส่งไปยังเกมของคุณ เซิร์ฟเวอร์

PlayGamesPlatform.Instance.RequestRecallAccess(
    recallAccess => {
        string recallSessionId = recallAccess.sessionId;
        // Send the recallSessionId to your game server
    });

ใช้ Recall API ภายในเซิร์ฟเวอร์เกม

หลังจากกำหนดค่าเซิร์ฟเวอร์และไคลเอ็นต์แล้ว คุณจะส่ง recallSessionID ได้ จากไคลเอ็นต์เกมไปยังเซิร์ฟเวอร์เกม และทำตามคำแนะนำด้านล่างเพื่อเริ่มต้น ใช้ Java API เพื่อจัดเก็บ เรียก หรือลบโทเค็น Recall ฝั่งเซิร์ฟเวอร์

โทเค็นสำหรับจัดเก็บ

ผู้ใช้ คุณจัดเก็บลักษณะตัวตนและโทเค็นเกมได้โดยใช้ LinkPersonaRequest ออบเจ็กต์ คุณต้องใช้ GoogleCredential เพื่อเรียกใช้ Google API (ดูการโทร Google API สำหรับบริบท) โปรดทราบว่าตาม 1:1 Cardinality ข้อจำกัด คุณสามารถลิงก์ได้เพียง 1 ลักษณะตัวตนเท่านั้น ไปยังโปรไฟล์ PGS ทีละโปรไฟล์ (และในทางกลับกัน) คุณควรตั้งค่าความละเอียด ในกรณีที่ลิงก์โปรไฟล์ PGS นี้กับลักษณะตัวตนอื่นแล้ว

นอกจากนี้ คุณยังเลือกตั้งค่า TTL บนโทเค็นได้ ซึ่งจะประกาศระยะเวลา โทเค็นนี้ถูกต้องโดยใช้ ระยะเวลา ออบเจ็กต์ คุณอาจเลือกตั้งค่านี้โดยใช้ SetTtl() (ดังที่แสดงด้านล่าง) ซึ่ง วันหมดอายุจากระยะเวลาที่ระบุไว้ในวิธีการ หรือ setExpireTime() ซึ่งช่วยให้คุณตั้งเวลาที่โทเค็นจะหมดอายุได้

คุณต้องเข้ารหัสลักษณะตัวตนและโทเค็นเกม โดยโทเค็นจะต้องไม่เป็นส่วนตัว ข้อมูลที่ระบุตัวตนได้ สตริงลักษณะตัวตนและโทเค็นมีได้สูงสุด 256 สตริง สามารถมีอักขระได้ไม่เกิน 20 ตัว และสามารถจัดเก็บโทเค็นหรือลักษณะตัวตนได้สูงสุด 20 ตัวต่อ ผู้เล่นต่อเกม

สามารถจัดเก็บโทเค็นได้เพียง 1 รายการต่อบุคคลต่อผู้เล่นในช่วงเวลาที่กำหนด กำลังพยายาม จัดเก็บโทเค็นอื่นที่มีลักษณะตัวตนเดียวกันจะแทนที่โทเค็นเดิม

import com.google.api.services.games.Games.Recall.LinkPersona;
import com.google.api.services.games.model.LinkPersonaRequest;
import com.google.api.services.games.model.LinkPersonaResponse;
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
}

เรียกข้อมูลโทเค็น

การเรียกคืนโทเค็นมี 3 ตัวเลือกดังนี้ ขึ้นอยู่กับเกมของคุณ ความต้องการ คุณสามารถ ขอรายการต่อไปนี้

  • โทเค็นที่เชื่อมโยงกับเกมปัจจุบัน รวมถึงโทเค็น Recall ที่กำหนดขอบเขตระดับเกม
  • โทเค็นสุดท้ายที่เก็บไว้ในเกมทั้งหมดที่เป็นของบัญชีนักพัฒนาแอป
  • จากรายชื่อเกมที่บัญชีนักพัฒนาแอปเป็นเจ้าของ โทเค็น Recall ทั้งหมด ที่เกี่ยวข้องกับแต่ละเกม

โทเค็นการเรียกคืนระดับเกม

หากต้องการเรียกโทเค็น Recall จากเกมปัจจุบัน ให้เรียก recallSessionId จากไคลเอ็นต์และส่งไปยัง retrieveTokens API:

import com.google.api.services.games.Games;
import com.google.api.services.games.model.RetrievePlayerTokensResponse;
import com.google.api.services.games.model.RecallToken;

// ...

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
}

โทเค็นการเรียกคืนล่าสุดในเกมทั้งหมดที่เป็นของบัญชีนักพัฒนาแอป

เพื่อเรียกดูโทเค็นล่าสุดที่จัดเก็บไว้ในเกมทั้งหมดที่นักพัฒนาเกมเป็นเจ้าของ ใน Google Play Console คุณจะต้องมีrecallSessionId จากไคลเอ็นต์และส่งไปยัง lastTokenFromAllDeveloperGames API เช่น ที่แสดงในข้อมูลโค้ดต่อไปนี้ คุณสามารถตรวจสอบ รหัสแอปพลิเคชันที่เชื่อมโยง ด้วยโทเค็นนี้

import com.google.api.services.games.Games;
import com.google.api.services.games.model.RetrieveDeveloperGamesLastPlayerTokenResponse;
import com.google.api.services.games.model.GamePlayerToken;
import com.google.api.services.games.model.RecallToken;

// ...

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

RetrieveDeveloperGamesLastPlayerTokenResponse response =
        gamesApi.recall().lastTokenFromAllDeveloperGames(recallSessionId)
        .execute();

if (response.hasGamePlayerToken()) {
    GamePlayerToken gamePlayerToken = response.getGamePlayerToken();

    // The ID of the application that the token is associated with.
    String applicationId = gamePlayerToken.getApplicationId();

    // Same string as was written in LinkPersona call.
    RecallToken recallToken = gamePlayerToken.getRecallToken();

    // Decrypt and recover in-game account.
}

โทเค็น Recall ทั้งหมดในรายการเกมที่ระบุที่เป็นของบัญชีนักพัฒนาแอป

เพื่อเรียกโทเค็นทั้งหมดที่เชื่อมโยงกับรายการเกมที่เป็นของ บัญชีนักพัฒนาแอปใน Google Play Console ให้ดาวน์โหลด recallSessionId จากไคลเอ็นต์และส่งไปยัง gamesPlayerTokens API ระบุ รายการรหัสแอปพลิเคชัน

import com.google.api.services.games.Games;
import com.google.api.services.games.model.RetrieveGamesPlayerTokensResponse;
import com.google.api.services.games.model.GamePlayerToken;
import com.google.api.services.games.model.RecallToken;

// ...

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

// Application IDs for which you would like to retrieve the recall tokens.
List<String> applicationIds = ...

RetrieveGamesPlayerTokensResponse response =
gamesApiClient
        .recall()
        .gamesPlayerTokens(recallSessionId)
        .setApplicationIds(applicationIds)
        .execute();

for (GamePlayerToken gamePlayerToken : response.getGamePlayerTokens()) {
    // The ID of the application that the token is associated with.
    String applicationId  = gamePlayerToken.getApplicationId();


    // Same string as was written in LinkPersona call.
    RecallToken recallToken = gamePlayerToken.getRecallToken();
    
    // Decrypt and recover in-game account.
}

ลบโทเค็นการเรียกคืน

หากจำเป็น คุณยังลบโทเค็นการเรียกคืนได้ด้วยการเรียกต่อไปนี้

import com.google.api.services.games.Games;
import com.google.api.services.games.model.UnlinkPersonaRequest;
import com.google.api.services.games.model.UnlinkPersonaResponse;

// ...

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();

boolean unlinked = unlinkPersonaResponse.isUnlinked();

เปิดใช้โหมดไม่มีโปรไฟล์

คุณเปิดใช้ฟังก์ชันการทำงานที่จำกัดของ Recall API ได้ สำหรับผู้ใช้ที่ไม่มีโปรไฟล์ PGS ให้ทำตามขั้นตอนต่อไปนี้

  1. เปิดใช้การเรียกคืนแบบไร้โปรไฟล์สำหรับโปรเจ็กต์เกม PGS ในนักพัฒนาแอป Play คอนโซล เลือกตัวเลือกที่มีข้อความว่า &quot;เปิด
พื้นที่เก็บข้อมูล&quot;
  2. โปรดอ่านข้อกำหนดเพิ่มเติมที่อธิบายภายหลังในส่วนนี้
  3. เพิ่มแท็กข้อมูลเมตาต่อไปนี้ลงในแอปของคุณ ไฟล์ Manifest
<meta-data
  android:name="com.google.android.gms.games.PROFILELESS_RECALL_ENABLED"
  android:value="true" />

ข้อกำหนดเพิ่มเติม

นอกเหนือจากการใช้ ข้อกำหนดในการให้บริการเกมของ Play แล้ว การให้บริการ แสดงว่าคุณยอมรับว่า คุณใช้ Recall API สําหรับผู้ใช้ที่ไม่มีโปรไฟล์ PGS ซึ่งจะช่วยให้แชร์ได้ ข้อมูลของผู้ใช้ปลายทางกับ Google หากไม่มีโปรไฟล์บริการเกมของ Play ก่อนที่จะแชร์ข้อมูลดังกล่าวกับ Google คุณต้องให้ผู้ใช้ปลายทาง ประกาศที่เหมาะสมซึ่งอธิบาย 1) การแชร์ข้อมูลของคุณกับ Google เพื่อให้ ของ Play Games ฟีเจอร์การลิงก์บัญชี 2) ความพร้อมใช้งานของการตั้งค่าที่ต้องจัดการ เช่น การแชร์ผ่านการตั้งค่า Play Games และ 3) การประมวลผลของ ข้อมูลดังกล่าวภายใต้นโยบายความเป็นส่วนตัวของ Google นโยบาย และรับผู้ใช้ปลายทางที่เหมาะสม ความยินยอมสำหรับการแชร์ดังกล่าวที่เป็นไปตามข้อกำหนดทางกฎหมายที่เกี่ยวข้องทั้งหมด