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

คุณสามารถใช้บริการเกมของ Google Play เพื่อให้ผู้เล่นลงชื่อเข้าใช้เกม Android ที่สร้างขึ้นใน Firebase ได้ หากต้องการใช้การลงชื่อเข้าใช้บริการเกมของ Google Play ด้วย Firebase ให้ผู้เล่นลงชื่อเข้าใช้ด้วยบริการเกมของ Google Play ก่อน แล้วขอรหัสการให้สิทธิ์ 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 Authentication สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารี

    นอกจากนี้ คุณยังต้องเพิ่ม SDK บริการ Google Play ลงในแอปด้วย ซึ่งเป็นส่วนหนึ่งของการตั้งค่าFirebase Authentication

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:34.10.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.5.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:24.0.1")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:21.5.1")
    }

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

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

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

    ./gradlew signingReport

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

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

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

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

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

กำหนดค่า Play Games services ด้วยข้อมูลแอป Firebase

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

  2. ในส่วน เพิ่มจำนวนผู้ใช้ ให้คลิก Play Games services > การตั้งค่าและการจัดการ > การกำหนดค่า

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

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

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

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

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

ขั้นแรก ให้ผสานรวมการลงชื่อเข้าใช้ Play Games เข้ากับแอป ดูวิธีการทั้งหมดได้ที่ ลงชื่อเข้าใช้เกม Android

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

Kotlin

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

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

Java

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

Kotlin

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

// 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 และสามารถใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ ได้

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

Kotlin

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

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

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

หากต้องการออกจากระบบผู้ใช้ ให้เรียกใช้ FirebaseAuth.signOut() ดังนี้

Kotlin

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();