คุณสามารถใช้บริการเกมของ Google Play เพื่อลงชื่อเข้าใช้เกม Android ที่สร้างบน Firebase ให้กับผู้เล่น หากต้องการใช้การลงชื่อเข้าใช้บริการ Google Play Games ด้วย Firebase ก่อนอื่นให้ลงชื่อเข้าใช้โปรแกรมเล่นด้วย Google Play Games แล้วขอรหัสการให้สิทธิ์ OAuth 2.0 จากนั้นส่งรหัสการตรวจสอบสิทธิ์ไปยัง PlayGamesAuthProvider
เพื่อสร้างข้อมูลเข้าสู่ระบบ Firebase ซึ่งคุณใช้ตรวจสอบสิทธิ์กับ Firebase ได้
ก่อนเริ่มต้น
ตั้งค่าโปรเจ็กต์ Android
เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้ทำ
ในไฟล์ 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.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:21.2.0") }การใช้ 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.2.0") }
ตั้งค่าโปรเจ็กต์ Firebase
ตั้งค่าลายนิ้วมือ SHA-1 ของเกมจากหน้าการตั้งค่าของคอนโซล Firebase
คุณรับแฮช SHA ของใบรับรองที่ลงนามได้ด้วยคำสั่ง Gradle
signingReport
./gradlew signingReport
เปิดใช้ Google Play Games เป็นผู้ให้บริการการลงชื่อเข้าใช้
ค้นหารหัสไคลเอ็นต์ของเว็บเซิร์ฟเวอร์และรหัสลับไคลเอ็นต์ของโปรเจ็กต์ รหัสไคลเอ็นต์ของเว็บเซิร์ฟเวอร์จะระบุโปรเจ็กต์ Firebase ของคุณให้กับเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของ Google Play
วิธีหาค่าเหล่านี้มีดังนี้
- เปิดโปรเจ็กต์ Firebase ในหน้าข้อมูลเข้าสู่ระบบของคอนโซล Google API
- ในส่วนรหัสไคลเอ็นต์ OAuth 2.0 ให้เปิดหน้ารายละเอียดไคลเอ็นต์เว็บ (สร้างอัตโนมัติโดยบริการของ Google) หน้านี้จะแสดงรหัสไคลเอ็นต์และรหัสลับ ของเว็บเซิร์ฟเวอร์ของคุณ
จากนั้นเปิดส่วนการตรวจสอบสิทธิ์ในคอนโซล Firebase
ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้ผู้ให้บริการการลงชื่อเข้าใช้ Play Games คุณจะต้องระบุรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์ของเว็บเซิร์ฟเวอร์ของโปรเจ็กต์ ซึ่งได้มาจากคอนโซล API
กำหนดค่าบริการเกมของ Play ด้วยข้อมูลแอป Firebase
ใน Google Play Console ให้เปิดแอป Google Play หรือสร้างแอป
ในส่วนขยายการมองเห็น ให้คลิก บริการเกมของ Play > การตั้งค่าและการจัดการ > การกำหนดค่า
คลิกใช่ เกมของฉันใช้ Google APIs อยู่แล้ว เลือกโปรเจ็กต์ Firebase จากรายการ แล้วคลิกใช้
ในหน้าการกำหนดค่าบริการเกมของ Play ให้คลิกเพิ่มเอกสารรับรอง
- เลือกประเภทเซิร์ฟเวอร์เกม
- ในช่องไคลเอ็นต์ OAuth ให้เลือกรหัสไคลเอ็นต์ของเว็บของโปรเจ็กต์ โปรดตรวจสอบว่านี่คือรหัสไคลเอ็นต์เดียวกับที่คุณระบุไว้เมื่อเปิดใช้ การลงชื่อเข้าใช้ Play Games
- บันทึกการเปลี่ยนแปลง
ที่ยังอยู่ในหน้าการกำหนดค่าบริการเกมของ Play ให้คลิกเพิ่มข้อมูลเข้าสู่ระบบอีกครั้ง
- เลือกประเภท Android
- ในช่องไคลเอ็นต์ OAuth ให้เลือกรหัสไคลเอ็นต์ Android ของโปรเจ็กต์ (หากไม่เห็นรหัสไคลเอ็นต์ Android โปรดตรวจสอบว่าได้ตั้งค่าลายนิ้วมือ SHA-1 ของเกมในคอนโซล Firebase)
- บันทึกการเปลี่ยนแปลง
ในหน้าผู้ทดสอบ ให้เพิ่มอีเมลของผู้ใช้ที่จำเป็นต้องลงชื่อเข้าใช้เกมก่อนที่จะเผยแพร่ใน 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
- ก่อนอื่น ให้รับอินสแตนซ์ที่แชร์ของออบเจ็กต์
FirebaseAuth
ในเมธอดonCreate
ของกิจกรรมการลงชื่อเข้าใช้ ดังนี้
Kotlin+KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
- เมื่อเริ่มต้นกิจกรรม ให้ตรวจสอบว่าโปรแกรมเล่นได้ลงชื่อเข้าใช้ด้วย 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.
- หลังจากที่ผู้เล่นลงชื่อเข้าใช้ด้วย 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 และกฎการรักษาความปลอดภัยของ Cloud Storage คุณสามารถดูรหัสผู้ใช้ที่ไม่ซ้ำของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth
และใช้ในการควบคุมข้อมูลที่ผู้ใช้เข้าถึงได้
หากต้องการรับข้อมูลโปรแกรมเล่น Play Games ของผู้ใช้หรือเข้าถึงบริการเกมของ Play ให้ใช้ API ที่ Google Play Games SDK มีให้
หากต้องการนำผู้ใช้ออกจากระบบ ให้โทรหา FirebaseAuth.signOut()
Kotlin+KTX
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();