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

Android पर Google Play गेम्स सेवाओं का उपयोग करके प्रमाणित करें

आप Firebase पर निर्मित Android गेम में खिलाड़ियों को साइन इन करने के लिए Google Play गेम्स सेवाओं का उपयोग कर सकते हैं। Firebase के साथ Google Play गेम्स सेवाओं के साइन-इन का उपयोग करने के लिए, पहले खिलाड़ी को Google Play गेम्स के साथ साइन इन करें, और ऐसा करने पर OAuth 2.0 प्रमाणीकरण कोड का अनुरोध करें। फिर, Firebase क्रेडेंशियल जनरेट करने के लिए PlayGamesAuthProvider को प्रमाणन कोड पास करें, जिसका उपयोग आप Firebase के साथ प्रमाणित करने के लिए कर सकते हैं।

शुरू करने से पहले

अपना Android प्रोजेक्ट सेट करें

  1. यदि आपने पहले से नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें

  2. अपने मॉड्यूल (ऐप-लेवल) ग्रैडल फ़ाइल (आमतौर पर <project>/<app-module>/build.gradle ) में, फायरबेस ऑथेंटिकेशन एंड्रॉइड लाइब्रेरी के लिए निर्भरता जोड़ें। हम लाइब्रेरी वर्ज़निंग को नियंत्रित करने के लिए Firebase Android BoM का उपयोग करने की सलाह देते हैं।

    साथ ही, फायरबेस ऑथेंटिकेशन सेट अप करने के हिस्से के रूप में, आपको अपने ऐप में Google Play services SDK को जोड़ना होगा।

    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 पुस्तकालयों के संगत संस्करणों का उपयोग करेगा।

    (वैकल्पिक) BoM का उपयोग किए बिना Firebase लाइब्रेरी निर्भरताएँ जोड़ें

    यदि आप Firebase BoM का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक Firebase लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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 पुस्तकालयों के संगत संस्करणों का उपयोग करेगा।

    (वैकल्पिक) BoM का उपयोग किए बिना Firebase लाइब्रेरी निर्भरताएँ जोड़ें

    यदि आप Firebase BoM का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक Firebase लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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'
    }

अपना फायरबेस प्रोजेक्ट सेट करें

  1. Firebase कंसोल के सेटिंग पेज से अपने गेम का SHA-1 फ़िंगरप्रिंट सेट करें।

    आप ग्रेडल signingReport कमांड के साथ अपने हस्ताक्षर प्रमाणपत्र का SHA हैश प्राप्त कर सकते हैं:

    ./gradlew signingReport

  2. साइन-इन प्रदाता के रूप में Google Play गेम्स को सक्षम करें:

    1. अपने प्रोजेक्ट का वेब सर्वर क्लाइंट आईडी और क्लाइंट सीक्रेट खोजें। वेब सर्वर क्लाइंट आईडी आपके फायरबेस प्रोजेक्ट को Google Play प्रमाणन सर्वरों की पहचान करता है।

      इन मूल्यों को खोजने के लिए:

      1. Google APIs कंसोल क्रेडेंशियल पेज में अपना Firebase प्रोजेक्ट खोलें।
      2. OAuth 2.0 क्लाइंट आईडी अनुभाग में, वेब क्लाइंट (Google सेवा द्वारा स्वतः निर्मित) विवरण पृष्ठ खोलें। यह पेज आपके वेब सर्वर क्लाइंट आईडी और सीक्रेट को सूचीबद्ध करता है।
    2. फिर, फायरबेस कंसोल में, प्रमाणीकरण अनुभाग खोलें।

    3. साइन इन विधि टैब पर, Play गेम्स साइन-इन प्रदाता को सक्षम करें। आपको अपने प्रोजेक्ट का वेब सर्वर क्लाइंट आईडी और क्लाइंट सीक्रेट बताना होगा, जो आपको एपीआई कंसोल से मिला था।

अपनी Firebase ऐप्लिकेशन जानकारी के साथ Play गेम्स सेवाएं कॉन्फ़िगर करें

  1. Google Play कंसोल में, अपना Google Play ऐप खोलें या एक बनाएं।

  2. आगे बढ़ें अनुभाग में, Play गेम्स सेवाएं > सेटअप और प्रबंधन > कॉन्फ़िगरेशन क्लिक करें.

  3. हाँ क्लिक करें, मेरा गेम पहले से ही Google API का उपयोग करता है , सूची से अपना Firebase प्रोजेक्ट चुनें और फिर उपयोग करें क्लिक करें.

  4. Play गेम्स सेवाओं के कॉन्फ़िगरेशन पृष्ठ पर, क्रेडेंशियल जोड़ें पर क्लिक करें।

    1. गेम सर्वर प्रकार का चयन करें।
    2. OAuth क्लाइंट फ़ील्ड में, अपने प्रोजेक्ट की वेब क्लाइंट आईडी चुनें। सुनिश्चित करें कि यह वही क्लाइंट आईडी है जिसे आपने प्ले गेम्स साइन-इन सक्षम करते समय निर्दिष्ट किया था।
    3. अपने परिवर्तन सहेजें।
  5. अभी भी Play गेम्स सेवाओं के कॉन्फ़िगरेशन पृष्ठ पर, फिर से क्रेडेंशियल जोड़ें पर क्लिक करें।

    1. Android प्रकार का चयन करें।
    2. OAuth क्लाइंट फ़ील्ड में, अपने प्रोजेक्ट की Android क्लाइंट आईडी चुनें। (यदि आपको अपनी Android क्लाइंट आईडी दिखाई नहीं देती है, तो सुनिश्चित करें कि आपने अपने गेम का SHA-1 फ़िंगरप्रिंट Firebase कंसोल में सेट किया है।)
    3. अपने परिवर्तन सहेजें।
  6. परीक्षक पृष्ठ पर, ऐसे किसी भी उपयोगकर्ता के ईमेल पते जोड़ें, जिन्हें आपके गेम को Play Store पर रिलीज़ करने से पहले उसमें साइन इन करने में सक्षम होना आवश्यक है।

Play - गेम्स साइन-इन को अपने गेम में एकीकृत करें

सबसे पहले, Play - गेम्स साइन-इन को अपने ऐप में एकीकृत करें। संपूर्ण निर्देशों के लिए 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 विधि में पास करना होगा। यह वह आईडी है जिसे आपने Firebase कंसोल में Play गेम्स साइन-इन सक्षम करते समय प्रदान किया था।

फायरबेस के साथ प्रमाणित करें

आपके द्वारा अपने ऐप में Play गेम्स साइन-इन जोड़ने के बाद, आपको Google खाता क्रेडेंशियल्स का उपयोग करने के लिए Firebase सेट अप करना होगा, जो आपको तब मिलता है जब कोई खिलाड़ी Play गेम्स के साथ सफलतापूर्वक साइन इन करता है।

  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. अपनी गतिविधि प्रारंभ करते समय, यह देखने के लिए जांचें कि क्या खिलाड़ी पहले से ही फायरबेस के साथ साइन इन है:

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 गेम्स में या तो चुपचाप या इंटरैक्टिव रूप से साइन इन करने के बाद, 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 गेम्स आईडी से लिंक किया जाता है। यह नया खाता आपके फायरबेस प्रोजेक्ट के हिस्से के रूप में संग्रहीत है, और इसका उपयोग आपके प्रोजेक्ट में प्रत्येक ऐप में उपयोगकर्ता की पहचान करने के लिए किया जा सकता है।

अपने गेम में, आप FirebaseUser ऑब्जेक्ट से उपयोगकर्ता का Firebase UID प्राप्त कर सकते हैं:

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

आपके फायरबेस रीयलटाइम डेटाबेस और क्लाउड स्टोरेज सुरक्षा नियमों में, आप auth चर से साइन-इन किए गए उपयोगकर्ता की विशिष्ट उपयोगकर्ता आईडी प्राप्त कर सकते हैं, और इसका उपयोग यह नियंत्रित करने के लिए कर सकते हैं कि उपयोगकर्ता किस डेटा तक पहुंच सकता है।

किसी उपयोगकर्ता की Play गेम्स प्लेयर जानकारी प्राप्त करने या Play गेम्स सेवाओं तक पहुँचने के लिए, Google Play गेम्स SDK द्वारा प्रदान किए गए API का उपयोग करें।

किसी उपयोगकर्ता को साइन आउट करने के लिए, FirebaseAuth.signOut() पर कॉल करें:

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();