การเข้าถึงบริการเกมของ Google Play ฝั่งเซิร์ฟเวอร์

เราขอแนะนำให้ใช้ PgsGamesSignInClient เพื่อตรวจสอบสิทธิ์ผู้เล่นและส่งข้อมูลประจำตัวของผู้เล่นไปยังเซิร์ฟเวอร์แบ็กเอนด์อย่างปลอดภัย ซึ่งจะช่วยให้เกมดึงข้อมูลประจำตัวและ ข้อมูลอื่นๆ ของผู้เล่นได้อย่างปลอดภัยโดยไม่เสี่ยงต่อการถูกดัดแปลงขณะส่งผ่าน อุปกรณ์

เมื่อผู้เล่นตรวจสอบสิทธิ์สำเร็จแล้ว คุณจะขอรหัสแบบใช้ครั้งเดียวพิเศษ (เรียกว่ารหัสการให้สิทธิ์เซิร์ฟเวอร์) จาก Native SDK (เบต้า) ของบริการเกมของ Play เวอร์ชัน 2 ได้ ซึ่งไคลเอ็นต์จะส่งไปยังเซิร์ฟเวอร์ จากนั้นในเซิร์ฟเวอร์ ให้แลกรหัสการให้สิทธิ์เซิร์ฟเวอร์เป็นโทเค็น OAuth 2.0 ที่เซิร์ฟเวอร์ใช้เพื่อทำการเรียกไปยัง Google Play Games Services API ได้

ดูคำแนะนำเพิ่มเติมเกี่ยวกับการเพิ่มการตรวจสอบสิทธิ์ในเกมได้ที่ การตรวจสอบสิทธิ์แพลตฟอร์ม

คุณต้องทำตามขั้นตอนต่อไปนี้เพื่อเข้าถึงแบบออฟไลน์

  1. ใน Google Play Console ให้สร้างข้อมูลเข้าสู่ระบบสำหรับเซิร์ฟเวอร์เกม ประเภทไคลเอ็นต์ OAuth ของข้อมูลเข้าสู่ระบบจะเป็น "เว็บ"
  2. ในแอป Android: ในขั้นตอนการตรวจสอบสิทธิ์แพลตฟอร์ม ให้ขอรหัสการตรวจสอบสิทธิ์เซิร์ฟเวอร์สำหรับข้อมูลเข้าสู่ระบบของเซิร์ฟเวอร์ แล้วส่งรหัสดังกล่าวไปยังเซิร์ฟเวอร์ PgsGamesSignInClient สามารถขอขอบเขต OAuth 2.0 ได้ 3 รายการเมื่อขอสิทธิ์เข้าถึงฝั่งเซิร์ฟเวอร์ไปยังเว็บ API ของบริการเกมของ Play ขอบเขตที่ไม่บังคับคือ PGS_AUTH_SCOPE_EMAIL, PGS_AUTH_SCOPE_PROFILE และ PGS_AUTH_SCOPE_OPENID ขอบเขตเริ่มต้น 2 รายการคือ DRIVE_APPFOLDER และ GAMES_LITE
  3. ในเซิร์ฟเวอร์เกม ให้แลกรหัสการให้สิทธิ์เซิร์ฟเวอร์เป็นโทเค็นการเข้าถึง OAuth โดยใช้บริการการตรวจสอบสิทธิ์ของ Google แล้วใช้โทเค็นนี้เพื่อเรียกใช้ REST API ของบริการเกมของ Play

ก่อนเริ่มต้น

ก่อนอื่นคุณต้องเพิ่มเกมใน Google Play Console ตามที่อธิบายไว้ในหัวข้อตั้งค่าบริการเกมของ Google Play ด้วยเกมของคุณ

สร้างเว็บแอปฝั่งเซิร์ฟเวอร์

บริการเกมของ Google Play ไม่รองรับแบ็กเอนด์สำหรับเกมบนเว็บ แต่เราจะให้การสนับสนุนเซิร์ฟเวอร์แบ็กเอนด์ สำหรับเซิร์ฟเวอร์ของเกม Android

หากต้องการใช้ REST API สำหรับบริการเกมของ Google Play ในแอปฝั่งเซิร์ฟเวอร์ ให้ทำตามขั้นตอนต่อไปนี้

  1. เลือกเกมใน Google Play Console
  2. ไปที่บริการเกมของ Play > การตั้งค่าและการจัดการ > การกำหนดค่า
  3. เลือกเพิ่มข้อมูลเข้าสู่ระบบเพื่อไปยังหน้าเพิ่มข้อมูลเข้าสู่ระบบ เลือกเซิร์ฟเวอร์เกมเป็นประเภทข้อมูลเข้าสู่ระบบ แล้วไปที่ส่วนการให้สิทธิ์
    1. หากเซิร์ฟเวอร์เกมมีรหัสไคลเอ็นต์ OAuth อยู่แล้ว ให้เลือกจาก เมนูแบบเลื่อนลง หลังจากบันทึกการเปลี่ยนแปลงแล้ว ให้ไปที่ส่วนถัดไป
    2. หากยังไม่มีรหัสไคลเอ็นต์ OAuth สำหรับเซิร์ฟเวอร์เกม คุณสามารถ สร้างรหัสได้
      1. คลิกสร้างไคลเอ็นต์ OAuth แล้วทำตามลิงก์สร้างรหัสไคลเอ็นต์ OAuth
      2. ซึ่งจะนำคุณไปยังหน้าสร้างรหัสไคลเอ็นต์ OAuth ของ Google Cloud Platform สำหรับโปรเจ็กต์ที่เชื่อมโยงกับเกม
      3. กรอกแบบฟอร์มของหน้าเว็บ แล้วคลิกสร้าง อย่าลืมตั้งค่า ประเภทแอปพลิเคชันเป็นเว็บแอปพลิเคชัน
      4. กลับไปที่ส่วนการให้สิทธิ์ของหน้าเพิ่มข้อมูลเข้าสู่ระบบ เลือกไคลเอ็นต์ OAuth ที่สร้างขึ้นใหม่ แล้วบันทึกการเปลี่ยนแปลง

รับรหัสการตรวจสอบสิทธิ์ของเซิร์ฟเวอร์

วิธีดึงรหัสการให้สิทธิ์เซิร์ฟเวอร์ที่เกมใช้สำหรับโทเค็นการเข้าถึงในเซิร์ฟเวอร์แบ็กเอนด์

  1. โทรหา PgsGamesSignInClient_requestServerSideAccess จากไคลเอ็นต์
    1. โปรดตรวจสอบว่าคุณใช้รหัสไคลเอ็นต์ OAuth ที่ลงทะเบียนสำหรับเซิร์ฟเวอร์เกม ไม่ใช่รหัสไคลเอ็นต์ OAuth ของแอปพลิเคชัน Android
    2. (ไม่บังคับ) หากเซิร์ฟเวอร์เกมของคุณต้องมีการเข้าถึงแบบออฟไลน์ (การเข้าถึงระยะยาว โดยใช้โทเค็นการรีเฟรช) เพื่อเข้าถึงบริการเกมของ Play คุณสามารถตั้งค่าพารามิเตอร์ force_refresh_token เป็น "จริง" ได้
  2. (ไม่บังคับ) ในขั้นตอนการตรวจสอบสิทธิ์ ผู้ใช้ใหม่ควรเห็นหน้าจอความยินยอมเดียวสำหรับขอบเขตเพิ่มเติม เมื่อยอมรับความยินยอม คุณจะ ตั้งค่าพารามิเตอร์ PgsAuthScope scopes ด้วยขอบเขต OAuth PGS_AUTH_SCOPE_EMAIL PGS_AUTH_SCOPE_PROFILE และ PGS_AUTH_SCOPE_OPENID หากผู้ใช้ปฏิเสธความยินยอม ระบบจะส่งเฉพาะขอบเขตเริ่มต้น 2 รายการ DRIVE_APPFOLDER และ GAMES_LITE ไปยังแบ็กเอนด์

    หน้าจอขอความยินยอมสำหรับขอบเขต OAuth เพิ่มเติม
    หน้าจอขอความยินยอมสำหรับขอบเขต OAuth เพิ่มเติม (คลิกเพื่อขยาย)

     // #include "google/games/pgs_games_sign_in_client.h"
     // 1. Define the Callback
     // This function is called when the server-side access request completes.
     // It provides the authorization code (on success) or an error (on failure).
     void OnServerSideAccessCallback(void* context, PgsError error, const char* serverAuthCode) {
         if (error == PgsError_Success) {
             if (serverAuthCode != nullptr) {
                 __android_log_print(ANDROID_LOG_INFO, "Games",
                     "Received Server Auth Code: %s", serverAuthCode);
                 // Send 'serverAuthCode' to your backend server immediately.
                 // Your server will exchange this code for an OAuth access token.
             }
         } else {
             __android_log_print(ANDROID_LOG_ERROR, "Games",
              "Failed to get server auth code. Error: %d", error);
         }
     }
     // 2. Define the Wrapper Function
     void RequestServerAccess(PgsGamesSignInClient* signInClient) {
         if (signInClient == nullptr) {
             return;
         }
         // This must match the "Web client ID" from your Google Cloud Console
         // (linked to your Play Console Game Server Credential).
         const char* SERVER_CLIENT_ID = "xxxx";
         // Set to 'true' if your server needs a Refresh Token (long-lived access).
         // Set to 'false' if you only need an Access Token (short-lived).
         bool forceRefreshToken = false;
         // Call the API
         PgsGamesSignInClient_requestServerSideAccess(
            signInClient,
            SERVER_CLIENT_ID,
            forceRefreshToken,
            OnServerSideAccessCallback, // The callback defined
            nullptr                     // User context (optional, passed to callback)
         );
     }
     // 3. Example Usage
     void TriggerSignInProcess(PgsGamesClient* gamesClient) {
          // Obtain the Sign-In Client from the main Games Client
          PgsGamesSignInClient* signInClient = PgsGamesClient_getSignInClient(gamesClient);
          RequestServerAccess(signInClient);
     }
     

  3. ส่งโทเค็นรหัสการให้สิทธิ์ OAuth ไปยังเซิร์ฟเวอร์แบ็กเอนด์เพื่อแลกเปลี่ยน ยืนยันรหัสผู้เล่นกับ REST API ของบริการเกมของ Play แล้ว ตรวจสอบสิทธิ์กับเกมของคุณ

ส่งรหัสการตรวจสอบสิทธิ์ของเซิร์ฟเวอร์

ส่งรหัสการให้สิทธิ์เซิร์ฟเวอร์ไปยังเซิร์ฟเวอร์แบ็กเอนด์เพื่อแลกเปลี่ยนเป็นโทเค็นการเข้าถึงและโทเค็นรีเฟรช ใช้โทเค็นเพื่อการเข้าถึงเพื่อเรียกใช้ Play Games Services API ในนามของผู้เล่น และจัดเก็บโทเค็นรีเฟรช (ไม่บังคับ) เพื่อรับโทเค็นเพื่อการเข้าถึงใหม่เมื่อโทเค็นเพื่อการเข้าถึงหมดอายุ

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีการทำงานของรหัสผู้เล่นได้ที่รหัสผู้เล่นรุ่นใหม่

ข้อมูลโค้ดต่อไปนี้แสดงวิธีที่คุณอาจใช้โค้ดฝั่งเซิร์ฟเวอร์ในภาษาโปรแกรม C++ เพื่อแลกเปลี่ยนรหัสการให้สิทธิ์ของเซิร์ฟเวอร์เป็นโทเค็นการเข้าถึง

Java

/**
 * Exchanges the authcode for an access token credential. The credential
 * is associated with the given player.
 *
 * @param authCode - the non-null authcode passed from the client.
 * @param player   - the player object which the given authcode is
 *                 associated with.
 * @return the HTTP response code indicating the outcome of the exchange.
 */
private int exchangeAuthCode(String authCode, Player player) {
try {

    // The client_secret.json file is downloaded from the Google Cloud
    // console. This is used to identify your web application. The
    // contents of this file shouldn't be shared.

    File secretFile = new File("client_secret.json");

    // If we don't have the file, we can't access any APIs, so return
    // an error.
    if (!secretFile.exists()) {
        log("Secret file : " + secretFile
                .getAbsolutePath() + "  does not exist!");
        return HttpServletResponse.SC_FORBIDDEN;
    }

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
            JacksonFactory.getDefaultInstance(), new
            FileReader(secretFile));

    // Extract the application ID of the game from the client ID.
    String applicationId = extractApplicationId(clientSecrets
            .getDetails().getClientId());

    GoogleTokenResponse tokenResponse =
            new GoogleAuthorizationCodeTokenRequest(
            HTTPTransport,
            JacksonFactory.getDefaultInstance(),
            "https://oauth2.googleapis.com/token",
            clientSecrets.getDetails().getClientId(),
            clientSecrets.getDetails().getClientSecret(),
            authCode,
            "")
            .execute();

    TokenVerifier(tokenResponse);

    log("hasRefresh == " + (tokenResponse.getRefreshToken() != null));
    log("Exchanging authCode: " + authCode + " for token");
    Credential credential = new Credential
            .Builder(BearerToken.authorizationHeaderAccessMethod())
            .setJsonFactory(JacksonFactory.getDefaultInstance())
            .setTransport(HTTPTransport)
            .setTokenServerEncodedUrl("https://www.googleapis.com/oauth2/v4/token")
            .setClientAuthentication(new HttpExecuteInterceptor() {
                @Override
                public void intercept(HttpRequest request)
                        throws IOException {
                        }
            })
            .build()
            .setFromTokenResponse(tokenResponse);

    player.setCredential(credential);

    // Now that we have a credential, we can access the Games API.
    PlayGamesAPI api = new PlayGamesAPI(player, applicationId,
            HTTPTransport, JacksonFactory.getDefaultInstance());

    // Call the verify method, which checks that the access token has
    // access to the Games API, and that the Player ID used by the
    // client matches the playerId associated with the accessToken.
    boolean ok = api.verifyPlayer();

    // Call a Games API on the server.
    if (ok) {
        ok = api.updatePlayerInfo();
        if (ok) {
            // persist the player.
            savePlayer(api.getPlayer());
        }
    }

    return ok ? HttpServletResponse.SC_OK :
            HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

  } catch (IOException e) {
    e.printStackTrace();
  }
  return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}

คุณสามารถดึงขอบเขต OAuth ได้โดยใช้ไลบรารีของไคลเอ็นต์ Google API ใน Java หรือ Python เพื่อรับออบเจ็กต์ GoogleIdTokenVerifier ข้อมูลโค้ดต่อไปนี้ แสดงการใช้งานในภาษาโปรแกรม Java

Java

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;

/**
 * Gets the GoogleIdTokenVerifier object and additional OAuth scopes.
 * If additional OAuth scopes are not requested, the idToken will be null.
 *
 * @param tokenResponse - the tokenResponse passed from the exchangeAuthCode
 *                        function.
 *
 **/

void TokenVerifier(GoogleTokenResponse tokenResponse) {

    string idTokenString = tokenResponse.getIdToken();

    GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
        // Specify the WEB_CLIENT_ID of the app that accesses the backend:
        .setAudience(Collections.singletonList(WEB_CLIENT_ID))
        // Or, if multiple clients access the backend:
        //.setAudience(Arrays.asList(WEB_CLIENT_ID_1, WEB_CLIENT_ID_2, WEB_CLIENT_ID_3))
        .build();

    GoogleIdToken idToken = verifier.verify(idTokenString);

    // The idToken can be null if additional OAuth scopes are not requested.
    if (idToken != null) {
        Payload payload = idToken.getPayload();

    // Print user identifier
    String userId = payload.getSubject();
    System.out.println("User ID: " + userId);

    // Get profile information from payload
    String email = payload.getEmail();
    boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
    String name = (String) payload.get("name");
    String pictureUrl = (String) payload.get("picture");
    String locale = (String) payload.get("locale");
    String familyName = (String) payload.get("family_name");
    String givenName = (String) payload.get("given_name");

    // This ID is unique to each Google Account, making it suitable for use as
    // a primary key during account lookup. Email is not a good choice because
    // it can be changed by the user.
    String sub = payload.getSubject();

    // Use or store profile information
    // ...

    } else {
      System.out.println("Invalid ID token.");
    }
}

เรียกใช้ REST API จากเซิร์ฟเวอร์

ดูคำอธิบายการเรียก API ทั้งหมดที่ใช้ได้ที่ REST API สำหรับบริการเกมของ Google Play

ตัวอย่างการเรียก REST API ที่คุณอาจพบว่ามีประโยชน์ ได้แก่

ผู้เล่น

หากต้องการรับการตรวจสอบสิทธิ์ด้วยรหัสและข้อมูลโปรไฟล์ของผู้เล่น เรียกใช้ Players.get โดยใช้ 'me' เป็นรหัส

ความสำเร็จ

ดูรายละเอียดได้ที่คำแนะนำเกี่ยวกับรางวัลพิเศษ

  • หากต้องการดูรายการรางวัลพิเศษปัจจุบัน ให้เรียกใช้ AchievementDefinitions.list

  • รวมเข้ากับการเรียกใช้ Achievements.list เพื่อดูว่าผู้เล่นปลดล็อกรางวัลใดบ้าง

  • เรียกใช้ Achievements.unlock เพื่อปลดล็อกรางวัลพิเศษของผู้เล่น

  • เรียกใช้ Achievements.increment เพื่อรายงานความคืบหน้าของรางวัลพิเศษ และดูว่าผู้เล่นปลดล็อกรางวัลพิเศษแล้วหรือไม่

  • หากคุณกำลังแก้ไขข้อบกพร่องของเกมที่ยังไม่ถึงเวอร์ชันที่ใช้งานจริง คุณสามารถเรียกใช้ Achievements.reset หรือ Achievements.resetAll จาก Management API เพื่อรีเซ็ตรางวัลพิเศษให้กลับสู่สถานะเดิม