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

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

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:34.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.4.0")
    }

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

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

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

    โปรดทราบว่าหากคุณใช้ไลบรารี 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.0")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:21.4.0")
    }

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

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

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

    ./gradlew signingReport

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

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

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

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

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

กําหนดค่า 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 Games มีให้

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

Kotlin

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();