ตรวจสอบสิทธิ์โดยใช้บริการเกมของ Google Play บน Android

คุณใช้บริการเกมของ Google Play ให้ผู้เล่นลงชื่อเข้าใช้เกม Android ที่สร้างขึ้นใน Firebase ได้ หากต้องการลงชื่อเข้าใช้บริการเกมของ Google Play ด้วย Firebase ก่อนอื่นให้ลงชื่อเข้าใช้โปรแกรมเล่นด้วย Google Play Games แล้วขอรหัสการให้สิทธิ์ OAuth 2.0 จากนั้นส่งรหัสการตรวจสอบสิทธิ์ไปยัง PlayGamesAuthProvider เพื่อสร้างข้อมูลเข้าสู่ระบบ Firebase ซึ่งคุณจะใช้ตรวจสอบสิทธิ์กับ Firebase ได้

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

ตั้งค่าโปรเจ็กต์ Android

  1. เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้ทำ

  2. ในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยทั่วไปจะเป็น <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) ให้เพิ่มทรัพยากร Dependency สำหรับไลบรารีการตรวจสอบสิทธิ์ของ Firebase สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันไลบรารี

    นอกจากนี้ ในการตั้งค่าการตรวจสอบสิทธิ์ Firebase คุณต้องเพิ่ม SDK บริการ Google Play ลงในแอปด้วย

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.0.0"))
    
        // Add the dependency for the Firebase Authentication library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:21.1.1")
    }

    การใช้ Firebase Android BoM จะทำให้ แอปใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ

    (ทางเลือก) เพิ่มทรัพยากร Dependency ของไลบรารี Firebase โดยไม่ต้องใช้ BoM

    หากเลือกไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดทรัพยากร Dependency

    โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลายในแอป เราขอแนะนำให้ใช้ BoM ในการจัดการเวอร์ชันไลบรารีซึ่งจะดูแลให้ทุกเวอร์ชันทำงานร่วมกันได้

    dependencies {
        // Add the dependency for the Firebase Authentication library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth:23.0.0")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:21.1.1")
    }
    หากกำลังมองหาโมดูลไลบรารีสำหรับ Kotlin โดยเฉพาะ ตั้งแต่ เดือนตุลาคม 2023 (Firebase BoM 32.5.0) ทั้งนักพัฒนา Kotlin และ Java จะ ขึ้นอยู่กับโมดูลไลบรารีหลัก (ดูรายละเอียดได้ที่ คําถามที่พบบ่อยเกี่ยวกับโครงการริเริ่มนี้)

สร้างโปรเจ็กต์ Firebase

  1. ตั้งค่าลายนิ้วมือ SHA-1 ของเกมจากหน้าการตั้งค่าของคอนโซล Firebase

    คุณจะรับแฮช SHA ของใบรับรองที่มีการรับรองได้ด้วยคำสั่ง Gradle signingReport ซึ่งมีดังนี้

    ./gradlew signingReport

  2. เปิดใช้ Google Play Games เป็นผู้ให้บริการการลงชื่อเข้าใช้

    1. ค้นหารหัสไคลเอ็นต์สำหรับเว็บเซิร์ฟเวอร์และรหัสลับไคลเอ็นต์ของโปรเจ็กต์ รหัสไคลเอ็นต์ของเว็บเซิร์ฟเวอร์จะระบุโปรเจ็กต์ Firebase ของคุณกับเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของ Google Play

      วิธีค้นหาค่าเหล่านี้

      1. เปิดโปรเจ็กต์ Firebase ในหน้าข้อมูลเข้าสู่ระบบคอนโซล Google APIs
      2. ในส่วนรหัสไคลเอ็นต์ OAuth 2.0 ให้เปิดหน้ารายละเอียดเว็บไคลเอ็นต์ (สร้างอัตโนมัติโดยบริการของ Google) หน้านี้จะแสดงรหัสไคลเอ็นต์และรหัสลับของเว็บเซิร์ฟเวอร์
    2. จากนั้นเปิดส่วนการตรวจสอบสิทธิ์ในคอนโซล Firebase

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

กำหนดค่าบริการเกมของ Play ด้วยข้อมูลแอป Firebase ของคุณ

  1. ใน Google Play Console ให้เปิดแอป Google Play หรือสร้างแอป

  2. ในส่วนขยาย ให้คลิกบริการเกมของ Play > การตั้งค่าและการจัดการ > การกำหนดค่า

  3. คลิกใช่ เกมของฉันใช้ Google APIs อยู่แล้ว เลือกโปรเจ็กต์ Firebase จากรายการ แล้วคลิกใช้

  4. ในหน้าการกำหนดค่าบริการเกมของ Play ให้คลิกเพิ่มข้อมูลเข้าสู่ระบบ

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

    1. เลือกประเภท Android
    2. ในช่องไคลเอ็นต์ OAuth ให้เลือกรหัสไคลเอ็นต์ Android ของโปรเจ็กต์ (หากไม่เห็นรหัสไคลเอ็นต์ Android ให้ตรวจสอบว่าได้ตั้งค่าลายนิ้วมือ SHA-1 ของเกมในคอนโซล Firebase)
    3. บันทึกการเปลี่ยนแปลง
  6. ในหน้าผู้ทดสอบ ให้เพิ่มอีเมลของผู้ใช้ที่ต้องลงชื่อเข้าใช้เกมได้ก่อนที่จะเผยแพร่ใน Play Store

ผสานรวมการลงชื่อเข้าใช้ Play Games เข้ากับเกมของคุณ

ก่อนอื่น ให้รวมการลงชื่อเข้าใช้ Play Games เข้ากับแอปของคุณ ดู ลงชื่อเข้าใช้ Android Games สำหรับคำแนะนำทั้งหมด

ในการผสานรวม เมื่อสร้างออบเจ็กต์ GoogleSignInOptions ให้ใช้การกำหนดค่า DEFAULT_GAMES_SIGN_IN และเรียกใช้ requestServerAuthCode

Kotlin+KTX

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
    .requestServerAuthCode(getString(R.string.default_web_client_id))
    .build()

Java

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
        .requestServerAuthCode(getString(R.string.default_web_client_id))
        .build();

คุณต้องส่งรหัสไคลเอ็นต์ของเว็บเซิร์ฟเวอร์ไปยังเมธอด requestServerAuthCode นี่คือรหัสที่คุณระบุเมื่อเปิดใช้การลงชื่อเข้าใช้ Play Games ในคอนโซล Firebase

ตรวจสอบสิทธิ์ด้วย Firebase

หลังจากที่เพิ่มการลงชื่อเข้าใช้ Play Games ลงในแอป คุณจะต้องตั้งค่า Firebase ใช้ข้อมูลเข้าสู่ระบบบัญชี Google ที่คุณจะได้รับเมื่อผู้เล่นลงชื่อเข้าใช้ Play Games ได้สำเร็จ

  1. ก่อนอื่น ในเมธอด onCreate ของกิจกรรมการลงชื่อเข้าใช้ ให้รับอินสแตนซ์ที่แชร์ของออบเจ็กต์ FirebaseAuth ดังนี้

Kotlin+KTX

private lateinit var auth: FirebaseAuth
// ...
// Initialize Firebase Auth
auth = Firebase.auth

Java

private FirebaseAuth mAuth;
// ...
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
  1. เมื่อเริ่มกิจกรรม ให้ตรวจสอบว่าผู้เล่นลงชื่อเข้าใช้ด้วย Firebase แล้วหรือยัง

Kotlin+KTX

override fun onStart() {
    super.onStart()
    // Check if user is signed in (non-null) and update UI accordingly.
    val currentUser = auth.currentUser
    updateUI(currentUser)
}

Java

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}
If the player isn't signed in, present the player with your game's
signed-out experience, including the option to sign in.
  1. หลังจากที่ผู้เล่นลงชื่อเข้าใช้ด้วย Play Games แบบเงียบหรือแบบอินเทอร์แอกทีฟแล้ว ให้รับรหัสการตรวจสอบสิทธิ์จากออบเจ็กต์ GoogleSignInAccount แลกเปลี่ยนเป็น ข้อมูลเข้าสู่ระบบ Firebase และตรวจสอบสิทธิ์กับ Firebase โดยใช้ข้อมูลเข้าสู่ระบบของ Firebase โดยทำดังนี้

Kotlin+KTX

// Call this both in the silent sign-in task's OnCompleteListener and in the
// Activity's onActivityResult handler.
private fun firebaseAuthWithPlayGames(acct: GoogleSignInAccount) {
    Log.d(TAG, "firebaseAuthWithPlayGames:" + acct.id!!)

    val auth = Firebase.auth
    val credential = PlayGamesAuthProvider.getCredential(acct.serverAuthCode!!)
    auth.signInWithCredential(credential)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithCredential:success")
                val user = auth.currentUser
                updateUI(user)
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInWithCredential:failure", task.exception)
                Toast.makeText(
                    baseContext,
                    "Authentication failed.",
                    Toast.LENGTH_SHORT,
                ).show()
                updateUI(null)
            }

            // ...
        }
}

Java

// Call this both in the silent sign-in task's OnCompleteListener and in the
// Activity's onActivityResult handler.
private void firebaseAuthWithPlayGames(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithPlayGames:" + acct.getId());

    final FirebaseAuth auth = FirebaseAuth.getInstance();
    AuthCredential credential = PlayGamesAuthProvider.getCredential(acct.getServerAuthCode());
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = auth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }

                    // ...
                }
            });
}

หากโทรหา signInWithCredential สำเร็จ คุณจะใช้เมธอด getCurrentUser เพื่อรับข้อมูลบัญชีของผู้ใช้ได้

ขั้นตอนถัดไป

หลังจากผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก ระบบจะสร้างบัญชีผู้ใช้ใหม่และลิงก์กับรหัส Play Games ระบบจะจัดเก็บบัญชีใหม่นี้เป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ได้

ในเกม คุณจะได้รับ Firebase UID ของผู้ใช้จากออบเจ็กต์ FirebaseUser ดังนี้

Kotlin+KTX

val user = auth.currentUser
user?.let {
    val playerName = it.displayName

    // The user's Id, unique to the Firebase project.
    // Do NOT use this value to authenticate with your backend server, if you
    // have one; use FirebaseUser.getIdToken() instead.
    val uid = it.uid
}

Java

FirebaseUser user = mAuth.getCurrentUser();
String playerName = user.getDisplayName();

// The user's Id, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server, if you
// have one; use FirebaseUser.getIdToken() instead.
String uid = user.getUid();

คุณจะได้รับรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้รหัสดังกล่าวเพื่อควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้ในกฎการรักษาความปลอดภัยของ Firebase และ Cloud Storage

หากต้องการรับข้อมูลโปรแกรมเล่น Play Games ของผู้ใช้หรือเข้าถึงบริการเกมของ Play ให้ใช้ API ที่ Google Play Games SDK เตรียมไว้ให้

หากต้องการนำผู้ใช้ออกจากระบบ โปรดโทรหา FirebaseAuth.signOut()

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();