लिंक किए गए खाते में साइन इन करने पर, उन उपयोगकर्ताओं के लिए एक टैप से साइन इन करने की सुविधा चालू हो जाती है जिन्होंने पहले से अपना Google खाता, आपकी सेवा से जोड़ा हुआ है. इससे उपयोगकर्ताओं को बेहतर अनुभव मिलता है, क्योंकि वे अपना उपयोगकर्ता नाम और पासवर्ड फिर से डाले बिना, एक ही क्लिक से साइन इन कर सकते हैं. इससे उपयोगकर्ताओं के लिए, आपकी सेवा पर डुप्लीकेट खाते बनाने की संभावना भी कम हो जाती है.
Android के लिए, लिंक किए गए खाते में साइन इन की सुविधा, 'एक टैप से साइन इन' फ़्लो के हिस्से के तौर पर उपलब्ध है. इसका मतलब है कि अगर आपके ऐप्लिकेशन में पहले से ही One Tap सुविधा चालू है, तो आपको अलग से किसी लाइब्रेरी को इंपोर्ट करने की ज़रूरत नहीं है.
इस दस्तावेज़ में, आप लिंक किए गए खाते में साइन इन करने की सुविधा के लिए, अपने Android ऐप्लिकेशन में बदलाव करने का तरीका जानेंगे.
यह कैसे काम करता है
- आप 'एक टैप से साइन इन' फ़्लो के दौरान लिंक किए गए खाते दिखाने के लिए ऑप्ट-इन करते हैं.
- अगर उपयोगकर्ता Google पर साइन इन है और उसने आपकी सेवा पर अपना खाता Google खाते से जोड़ा है, तो हम आपको लिंक किए गए खाते के लिए एक आईडी टोकन भेजते हैं.
- उपयोगकर्ता को एक टैप करके साइन इन करने का अनुरोध दिखाया जाता है. इसमें, उपयोगकर्ता के लिंक किए गए खाते से आपकी सेवा में साइन इन करने का विकल्प भी होता है.
- अगर उपयोगकर्ता लिंक किए गए खाते के साथ जारी रखने का विकल्प चुनता है, तो उपयोगकर्ता का आईडी टोकन आपके ऐप्लिकेशन पर वापस आ जाता है. आप लॉग इन किए हुए उपयोगकर्ता की पहचान करने के लिए, दूसरे चरण में आपके सर्वर पर भेजे गए टोकन से मिलान करते हैं.
सेट अप
अपना डेवलपमेंट एनवायरमेंट सेट अप करें
अपने डेवलपमेंट होस्ट पर, Google Play की नई सेवाएं पाएं:
- Android SDK टूल खोलें.
SDK टूल में, Google Play सेवाएं ढूंढें.
अगर इन पैकेज की स्थिति इंस्टॉल नहीं की गई है, तो उन्हें चुनें और पैकेज इंस्टॉल करें पर क्लिक करें.
अपना ऐप्लिकेशन कॉन्फ़िगर करें
अपने प्रोजेक्ट-लेवल की
build.gradle
फ़ाइल में,buildscript
औरallprojects
, दोनों सेक्शन में Google's Maven डेटा स्टोर करने की जगह शामिल करें.buildscript { repositories { google() } } allprojects { repositories { google() } }
अपने मॉड्यूल की #
app/build.gradle
Google के साथ लिंक करें" एपीआई के लिए डिपेंडेंसी जोड़ेंdependencies { implementation 'com.google.android.gms:play-services-auth:20.2.0' }
जोड़े गए खाते के साइन इन के लिए अपने Android ऐप्लिकेशन में बदलाव करें
लिंक किए गए खाते के साइन इन फ़्लो के आखिर में, आपके ऐप्लिकेशन पर एक आईडी टोकन वापस कर दिया जाता है. उपयोगकर्ता को साइन इन करने से पहले, आईडी टोकन और #39 की इंटेग्रिटी की पुष्टि की जानी चाहिए.
नीचे दिया गया कोड सैंपल, फिर से पाने, आईडी टोकन की पुष्टि करने, और बाद में उपयोगकर्ता को साइन इन करने का तरीका बताता है.
साइन इन वाले इंटेंट का नतीजा पाने के लिए एक गतिविधि बनाएं
Kotlin
private val activityResultLauncher = registerForActivityResult( ActivityResultContracts.StartIntentSenderForResult()) { result -> if (result.resultCode == RESULT_OK) { try { val signInCredentials = Identity.signInClient(this) .signInCredentialFromIntent(result.data) // Review the Verify the integrity of the ID token section for // details on how to verify the ID token verifyIdToken(signInCredential.googleIdToken) } catch (e: ApiException) { Log.e(TAG, “Sign-in failed with error code:”, e) } } else { Log.e(TAG, “Sign-in failed”) } }
Java
private final ActivityResultLauncher<IntentSenderResult> activityResultLauncher = registerForActivityResult( new ActivityResultContracts.StartIntentSenderForResult(), result -> { If (result.getResultCode() == RESULT_OK) { try { SignInCredential signInCredential = Identity.getSignInClient(this) .getSignInCredentialFromIntent(result.getData()); verifyIdToken(signInCredential.getGoogleIdToken()); } catch (e: ApiException ) { Log.e(TAG, “Sign-in failed with error:”, e) } } else { Log.e(TAG, “Sign-in failed”) } });
साइन इन करने का अनुरोध तैयार करना
Kotlin
private val tokenRequestOptions = GoogleIdTokenRequestOptions.Builder() .supported(true) // Your server's client ID, not your Android client ID. .serverClientId(getString("your-server-client-id") .filterByAuthorizedAccounts(true) .associateLinkedAccounts("service-id-of-and-defined-by-developer", scopes) .build()
Java
private final GoogleIdTokenRequestOptions tokenRequestOptions = GoogleIdTokenRequestOptions.Builder() .setSupported(true) .setServerClientId("your-service-client-id") .setFilterByAuthorizedAccounts(true) .associateLinkedAccounts("service-id-of-and-defined-by-developer", scopes) .build()
साइन इन करने की मंज़ूरी बाकी है
Kotlin
Identity.signInClient(this) .beginSignIn( BeginSignInRequest.Builder() .googleIdTokenRequestOptions(tokenRequestOptions) .build()) .addOnSuccessListener{result -> activityResultLauncher.launch(result.pendingIntent.intentSender) } .addOnFailureListener {e -> Log.e(TAG, “Sign-in failed because:”, e) }
Java
Identity.getSignInClient(this) .beginSignIn( BeginSignInRequest.Builder() .setGoogleIdTokenRequestOptions(tokenRequestOptions) .build()) .addOnSuccessListener(result -> { activityResultLauncher.launch( result.getPendingIntent().getIntentSender()); }) .addOnFailureListener(e -> { Log.e(TAG, “Sign-in failed because:”, e); });
आईडी टोकन के सही होने की पुष्टि करना
To verify that the token is valid, ensure that the following criteria are satisfied:
- The ID token is properly signed by Google. Use Google's public keys
(available in
JWK or
PEM format)
to verify the token's signature. These keys are regularly rotated; examine
the
Cache-Control
header in the response to determine when you should retrieve them again. - The value of
aud
in the ID token is equal to one of your app's client IDs. This check is necessary to prevent ID tokens issued to a malicious app being used to access data about the same user on your app's backend server. - The value of
iss
in the ID token is equal toaccounts.google.com
orhttps://accounts.google.com
. - The expiry time (
exp
) of the ID token has not passed. - If you want to restrict access to only members of your G Suite domain,
verify that the ID token has an
hd
claim that matches your G Suite domain name.
Rather than writing your own code to perform these verification steps, we strongly
recommend using a Google API client library for your platform, or a general-purpose
JWT library. For development and debugging, you can call our tokeninfo
validation endpoint.
Google API क्लाइंट लाइब्रेरी का इस्तेमाल करना
प्रोडक्शन एनवायरमेंट में, Google आईडी टोकन की पुष्टि करने के लिए, Java Google API क्लाइंट लाइब्रेरी का इस्तेमाल करने का सुझाव दिया जाता है.
Java
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
...
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
// Specify the CLIENT_ID of the app that accesses the backend:
.setAudience(Collections.singletonList(CLIENT_ID))
// Or, if multiple clients access the backend:
//.setAudience(Arrays.asList(CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3))
.build();
// (Receive idTokenString by HTTPS POST)
GoogleIdToken idToken = verifier.verify(idTokenString);
if (idToken != null) {
Payload payload = idToken.getPayload();
// Print user identifier
String userId = payload.getSubject();
System.out.println("User ID: " + userId);
// Get profile information from payload
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
// Use or store profile information
// ...
} else {
System.out.println("Invalid ID token.");
}
GoogleIdTokenVerifier.verify()
तरीके से, JWT हस्ताक्षर, aud
दावा, iss
दावा, और exp
दावे की पुष्टि होती है.
अगर आप सिर्फ़ अपने G Suite डोमेन के सदस्यों तक ऐक्सेस को सीमित करना चाहते हैं, तो Payload.getHostedDomain()
तरीके से लौटाए गए डोमेन नाम की जांच करके भी hd
दावे की पुष्टि करें.
Calling the tokeninfo endpoint
An easy way to validate an ID token signature for debugging is to
use the tokeninfo
endpoint. Calling this endpoint involves an
additional network request that does most of the validation for you while you test proper
validation and payload extraction in your own code. It is not suitable for use in production
code as requests may be throttled or otherwise subject to intermittent errors.
To validate an ID token using the tokeninfo
endpoint, make an HTTPS
POST or GET request to the endpoint, and pass your ID token in the
id_token
parameter.
For example, to validate the token "XYZ123", make the following GET request:
https://oauth2.googleapis.com/tokeninfo?id_token=XYZ123
If the token is properly signed and the iss
and exp
claims have the expected values, you will get a HTTP 200 response, where the body
contains the JSON-formatted ID token claims.
Here's an example response:
{ // These six fields are included in all Google ID Tokens. "iss": "https://accounts.google.com", "sub": "110169484474386276334", "azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com", "aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com", "iat": "1433978353", "exp": "1433981953", // These seven fields are only included when the user has granted the "profile" and // "email" OAuth scopes to the application. "email": "testuser@gmail.com", "email_verified": "true", "name" : "Test User", "picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg", "given_name": "Test", "family_name": "User", "locale": "en" }
If you are a G Suite customer, you might also be interested in the hd
claim, which indicates the hosted domain of the user. This can be used to restrict access
to a resource to only members of certain domains. The absence of this claim indicates
that the user does not belong to a G Suite hosted domain.