หน้านี้อธิบายวิธีใช้ 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
});
Unity SDK
หากยังไม่ได้ดำเนินการ ให้ตั้งค่า 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 เพื่อจัดเก็บ เรียกข้อมูล หรือลบโทเค็นการเรียกคืน
ฝั่งเซิร์ฟเวอร์ได้
จัดเก็บโทเค็น
บัญชีผู้เล่นใน Google Play Games Recall API ประกอบด้วยข้อมูล 2 ส่วน ดังนี้
- เพอร์โซนาที่ทำหน้าที่เป็นตัวระบุแบบคงที่สำหรับบัญชีในเกม
- โทเค็นที่ใช้เป็นคีย์ในการลงนามผู้เล่นในบัญชีอย่างปลอดภัย
คุณจัดเก็บลักษณะตัวตนและโทเค็นของผู้ใช้ได้โดยใช้LinkPersonaRequest
ออบเจ็กต์ ใช้ GoogleCredential เพื่อเรียก Google APIs (ดูบริบทได้ที่การเรียก Google APIs) เพอร์โซนามีข้อจํากัดแบบคาร์ดินาลิตี 1:1 กล่าวคือ โปรไฟล์ PGS เดียวจะมีได้เพียงเพอร์โซนาเดียว และเพอร์โซนาจะมีได้ในโปรไฟล์ PGS เดียวเท่านั้น ตั้งค่านโยบายการแก้ไขลิงก์ที่ขัดแย้งกันเพื่อกำหนด
วิธีแก้ไขการละเมิดข้อจำกัดของ Cardinality แบบ 1:1
กำหนดเวลาหมดอายุของโทเค็น (ไม่บังคับ) ใช้ SetTtl() กับออบเจ็กต์
Durations เพื่อตั้งค่า Time to Live หรือระบุเวลาหมดอายุที่แน่นอน
ด้วย setExpireTime()
คุณต้องเข้ารหัสโทเค็นตัวตนและเกม และโทเค็นดังกล่าวต้องไม่มีข้อมูลส่วนบุคคลที่ระบุตัวบุคคลนั้นได้ สตริงของเพอร์โซนาและโทเค็นต้องมีความยาวไม่เกิน 256 อักขระ
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 ตัวเลือกในการดึงโทเค็นตามความต้องการของเกม คุณสามารถ ขอสิ่งต่อไปนี้ได้
- โทเค็นที่เชื่อมโยงกับเกมปัจจุบัน รวมถึงโทเค็นการเรียกคืนที่กำหนดขอบเขตไว้ที่เกม
- โทเค็นล่าสุดที่จัดเก็บไว้ในเกมทั้งหมดที่เป็นของบัญชีนักพัฒนาแอป
- รายการเกมที่เป็นของบัญชีนักพัฒนาแอป รวมถึงโทเค็นการเรียกคืนทั้งหมด ที่เชื่อมโยงกับแต่ละเกม
โทเค็นการเรียกคืนข้อมูลที่กำหนดขอบเขตเป็นเกม
หากต้องการเรียกโทเค็นการเรียกคืนจากเกมปัจจุบัน ให้รับ recallSessionId
จากไคลเอ็นต์และส่งไปยัง API retrieveTokens ดังนี้
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.
}
โทเค็นการเรียกคืนทั้งหมดในรายการเกมที่กำหนดซึ่งบัญชีนักพัฒนาแอปเป็นเจ้าของ
หากต้องการดึงโทเค็นทั้งหมดที่เชื่อมโยงกับรายการเกมซึ่งเป็นของบัญชีนักพัฒนาแอปของคุณใน 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 ได้โดยทำตามขั้นตอนต่อไปนี้
- เปิดใช้การเรียกคืนแบบไม่มีโปรไฟล์สำหรับโปรเจ็กต์เกม PGS ใน Play Developer
Console

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