คุณใช้บริการ Google Play Games เพื่อลงชื่อเข้าใช้เกม Android ที่สร้างบน Firebase ให้กับผู้เล่นได้ ในการใช้บริการลงชื่อเข้าใช้บริการ Google Play Games ด้วย Firebase ก่อนอื่นให้ลงชื่อเข้าใช้ผู้เล่นด้วย Google Play Games และขอรหัสตรวจสอบสิทธิ์ OAuth 2.0 เมื่อคุณดำเนินการดังกล่าว จากนั้นส่งรหัสตรวจสอบสิทธิ์ไปที่ PlayGamesAuthProvider
เพื่อสร้างข้อมูลรับรอง Firebase ซึ่งคุณสามารถใช้ตรวจสอบสิทธิ์กับ Firebase ได้
ก่อนจะเริ่ม
ตั้งค่าโปรเจ็กต์ Android ของคุณ
หากคุณยังไม่ได้ เพิ่ม Firebase ในโครงการ Android ของคุณ
ใช้ Firebase Android BoM ประกาศการพึ่งพาสำหรับไลบรารี Firebase Authentication Android ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ
app/build.gradle
)นอกจากนี้ ในการตั้งค่าการตรวจสอบสิทธิ์ Firebase คุณต้องเพิ่ม SDK บริการ Google Play ในแอปของคุณ
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // Declare 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 declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) ประกาศการพึ่งพาไลบรารี Firebase โดยไม่ ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดการพึ่งพา
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลายรายการ ในแอปของคุณ เราขอแนะนำให้ใช้ BoM เพื่อจัดการเวอร์ชันของไลบรารี ซึ่งจะทำให้มั่นใจได้ว่าทุกเวอร์ชันจะเข้ากันได้
dependencies { // Declare 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:21.0.7'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:30.3.1') // Declare 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 declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) ประกาศการพึ่งพาไลบรารี Firebase โดยไม่ ใช้ BoM
หากคุณเลือกที่จะไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดการพึ่งพา
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลายรายการ ในแอปของคุณ เราขอแนะนำให้ใช้ BoM เพื่อจัดการเวอร์ชันของไลบรารี ซึ่งจะทำให้มั่นใจได้ว่าทุกเวอร์ชันจะเข้ากันได้
dependencies { // Declare 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:21.0.7'
// Also declare the dependency for the Google Play services library and specify its version implementation 'com.google.android.gms:play-services-auth:20.2.0' }
ตั้งค่าโปรเจ็กต์ Firebase
ตั้งค่าลายนิ้วมือ SHA-1 ของเกมจากหน้า การตั้งค่า ของคอนโซล Firebase
คุณสามารถรับแฮช SHA ของใบรับรองการลงนามด้วยคำสั่ง
signingReport
:./gradlew signingReport
เปิดใช้งาน Google Play Games เป็นผู้ให้บริการลงชื่อเข้าใช้:
ค้นหารหัสไคลเอ็นต์เว็บเซิร์ฟเวอร์ของโปรเจ็กต์และข้อมูลลับไคลเอ็นต์ รหัสไคลเอ็นต์ของเว็บเซิร์ฟเวอร์ระบุโปรเจ็กต์ Firebase ของคุณไปยังเซิร์ฟเวอร์การตรวจสอบสิทธิ์ของ Google Play
หากต้องการค้นหาค่าเหล่านี้:
- เปิดโปรเจ็กต์ Firebase ในหน้าข้อมูลรับรอง คอนโซล Google APIs
- ในส่วน รหัสไคลเอ็นต์ OAuth 2.0 ให้เปิดหน้ารายละเอียด เว็บไคลเอ็นต์ (สร้างอัตโนมัติโดยบริการของ Google) หน้านี้แสดงรายการ ID และข้อมูลลับของไคลเอ็นต์เว็บเซิร์ฟเวอร์ของคุณ
จากนั้นใน คอนโซล Firebase ให้เปิดส่วนการ ตรวจสอบสิทธิ์
ในแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้ผู้ให้บริการลงชื่อเข้าใช้ Play Games คุณจะต้องระบุรหัสไคลเอ็นต์เว็บเซิร์ฟเวอร์ของโปรเจ็กต์และข้อมูลลับของไคลเอ็นต์ซึ่งได้รับจากคอนโซล API
กำหนดค่าบริการ Play Games ด้วยข้อมูลแอป Firebase ของคุณ
ใน Google Play Console ให้เปิดแอป Google Play ของคุณหรือสร้างใหม่
ในส่วน เติบโต ให้คลิก บริการ Play Games > การตั้งค่าและการจัดการ > การกำหนดค่า
คลิก ใช่ เกมของฉันใช้ Google API อยู่ แล้ว เลือกโปรเจ็กต์ Firebase ของคุณจากรายการ จากนั้นคลิก ใช้
ในหน้าการกำหนดค่าบริการ Play Games ให้คลิก เพิ่มข้อมูลรับรอง
- เลือกประเภท เซิร์ฟเวอร์เกม
- ในฟิลด์ ไคลเอ็นต์ OAuth ให้เลือกรหัสเว็บไคลเอ็นต์ของโปรเจ็กต์ ตรวจสอบว่ารหัสนี้เป็นรหัสไคลเอ็นต์เดียวกับที่คุณระบุเมื่อเปิดใช้การลงชื่อเข้าใช้ Play Games
- บันทึกการเปลี่ยนแปลงของคุณ
ยังอยู่ในหน้าการกำหนดค่าบริการ Play Games ให้คลิก เพิ่มข้อมูลรับรอง อีกครั้ง
- เลือกประเภท Android
- ในช่อง ไคลเอ็นต์ OAuth ให้เลือกรหัสไคลเอ็นต์ Android ของโปรเจ็กต์ (หากคุณไม่เห็นรหัสไคลเอ็นต์ Android ของคุณ ตรวจสอบให้แน่ใจว่าคุณได้ตั้งค่าลายนิ้วมือ SHA-1 ของเกมในคอนโซล Firebase)
- บันทึกการเปลี่ยนแปลงของคุณ
ในหน้าผู้ ทดสอบ ให้เพิ่มที่อยู่อีเมลของผู้ใช้ที่ต้องการลงชื่อเข้าใช้เกมของคุณก่อนที่จะเผยแพร่ใน Play Store
รวมการลงชื่อเข้าใช้ Play Games เข้ากับเกมของคุณ
ขั้นแรก รวมการลงชื่อเข้าใช้ Play Games เข้ากับแอปของคุณ ดู การลงชื่อเข้าใช้เกม Android สำหรับคำแนะนำที่สมบูรณ์
ในการผสานรวมของคุณ เมื่อคุณสร้างอ็อบเจ็กต์ GoogleSignInOptions
ให้ใช้การกำหนดค่า DEFAULT_GAMES_SIGN_IN
และการเรียก requestServerAuthCode
:
Java
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) .requestServerAuthCode(getString(R.string.default_web_client_id)) .build();
Kotlin+KTX
val gso = 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 ได้สำเร็จ
- ขั้นแรก ในวิธีการ
onCreate
ของกิจกรรมการลงชื่อเข้าใช้ รับอินสแตนซ์ที่ใช้ร่วมกันของวัตถุFirebaseAuth
:
Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin+KTX
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
- เมื่อเริ่มต้นกิจกรรมของคุณ ให้ตรวจสอบว่าผู้เล่นลงชื่อเข้าใช้ Firebase แล้วหรือไม่:
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); }
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) }
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:
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); } // ... } }); }
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) } // ... } }
หากการเรียก signInWithCredential
สำเร็จ คุณสามารถใช้เมธอด getCurrentUser
เพื่อรับข้อมูลบัญชีของผู้ใช้
ขั้นตอนถัดไป
หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับรหัส Play Games ของพวกเขา บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และใช้เพื่อระบุผู้ใช้ในทุกแอปในโปรเจ็กต์ของคุณ
ในเกมของคุณ คุณสามารถรับ Firebase UID ของผู้ใช้จากอ็อบเจ็กต์ FirebaseUser
:
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();
Kotlin+KTX
val user = auth.currentUser user?.let { val playerName = user.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 = user.uid }
ในฐานข้อมูล Firebase Realtime Database และ Cloud Storage Security Rules คุณสามารถรับ ID ผู้ใช้เฉพาะของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth
และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้
หากต้องการรับข้อมูลผู้เล่น Play Games ของผู้ใช้หรือเพื่อเข้าถึงบริการ Play Games ให้ใช้ API ที่จัดเตรียมโดย SDK ของ Google Play Games
หากต้องการออกจากระบบผู้ใช้ ให้เรียก FirebaseAuth.signOut()
:
Java
FirebaseAuth.getInstance().signOut();
Kotlin+KTX
Firebase.auth.signOut()