Catch up on highlights from Firebase at Google I/O 2023. Learn more

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

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

ก่อนที่คุณจะเริ่มต้น

ตั้งค่าโครงการ Android ของคุณ

  1. หากคุณยังไม่ได้ ดำเนินการ ให้เพิ่ม Firebase ในโครงการ Android ของคุณ

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

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

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:32.1.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-ktx'
    // Also add the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.5.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-ktx:22.0.0'
    // Also add the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.5.0'
    }

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:32.1.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:20.5.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:22.0.0'
    // Also add the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.5.0'
    }

ตั้งค่าโครงการ Firebase ของคุณ

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

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

    ./gradlew signingReport

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

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

      หากต้องการค้นหาค่าเหล่านี้:

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

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

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

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

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

  3. คลิก ใช่ เกมของฉันใช้ Google API อยู่แล้ว เลือกโครงการ Firebase ของคุณจากรายการ จากนั้นคลิก ใช้

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

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

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

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

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

ในการรวมระบบ เมื่อคุณสร้างอ็อบเจ็กต์ 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 นี่คือ ID ที่คุณระบุเมื่อเปิดใช้งานการลงชื่อเข้าใช้ 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();

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

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

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

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();