OAuth के साथ Google खाता लिंक करना

खाते, इंडस्ट्री स्टैंडर्ड OAuth 2.0 के इंप्लिसिट और ऑथराइज़ेशन कोड फ़्लो का इस्तेमाल करके लिंक किए जाते हैं.

आपकी सेवा में, OAuth 2.0 के मुताबिक अनुमति और टोकन एक्सचेंज एंडपॉइंट काम करने चाहिए.

इंप्लिसिट फ़्लो में, Google उपयोगकर्ता के ब्राउज़र में आपका ऑथराइज़ेशन एंडपॉइंट खोलता है. साइन इन करने के बाद, Google को लंबे समय तक इस्तेमाल किया जा सकने वाला ऐक्सेस टोकन वापस भेजें. अब यह ऐक्सेस टोकन, Google से भेजे गए हर अनुरोध में शामिल किया जाता है.

ऑथराइज़ेशन कोड फ़्लो में, आपको दो एंडपॉइंट की ज़रूरत होती है:

  • authorization एंडपॉइंट, जो उन उपयोगकर्ताओं को साइन-इन यूज़र इंटरफ़ेस दिखाता है जिन्होंने पहले से साइन इन नहीं किया है. ऑथराइज़ेशन एंडपॉइंट, कम समय के लिए मान्य ऑथराइज़ेशन कोड भी बनाता है. इससे, अनुरोध किए गए ऐक्सेस के लिए उपयोगकर्ताओं की सहमति रिकॉर्ड की जाती है.

  • टोकन एक्सचेंज एंडपॉइंट, जो दो तरह के एक्सचेंज के लिए ज़िम्मेदार होता है:

    1. यह ऑथराइज़ेशन कोड को लंबे समय तक चलने वाले रीफ़्रेश टोकन और कम समय तक चलने वाले ऐक्सेस टोकन से बदलता है. यह एक्सचेंज तब होता है, जब उपयोगकर्ता खाता लिंक करने की प्रोसेस पूरी करता है.
    2. यह लंबे समय तक चलने वाले रीफ़्रेश टोकन को कम समय तक चलने वाले ऐक्सेस टोकन से बदलता है. यह एक्सचेंज तब होता है, जब Google को नए ऐक्सेस टोकन की ज़रूरत होती है, क्योंकि उसके पास मौजूद टोकन की समयसीमा खत्म हो गई होती है.

OAuth 2.0 फ़्लो चुनना

इंप्लिसिट फ़्लो को लागू करना आसान है. हालांकि, Google का सुझाव है कि इंप्लिसिट फ़्लो से जारी किए गए ऐक्सेस टोकन की समयसीमा कभी खत्म नहीं होनी चाहिए. ऐसा इसलिए होता है, क्योंकि टोकन की समयसीमा खत्म होने के बाद, उपयोगकर्ता को इंप्लिसिट फ़्लो के साथ अपना खाता फिर से लिंक करना पड़ता है. अगर आपको सुरक्षा की वजहों से टोकन की समयसीमा खत्म होने की सुविधा चाहिए, तो हमारा सुझाव है कि आप ऑथराइज़ेशन कोड फ़्लो का इस्तेमाल करें.

डिज़ाइन से जुड़े दिशा-निर्देश

इस सेक्शन में, OAuth लिंक करने के फ़्लो के लिए होस्ट की गई उपयोगकर्ता स्क्रीन के डिज़ाइन से जुड़ी ज़रूरी शर्तों और सुझावों के बारे में बताया गया है. Google के ऐप्लिकेशन से कॉल किए जाने के बाद, आपका प्लैटफ़ॉर्म उपयोगकर्ता को'Google में साइन इन करें' पेज और खाता लिंक करने की सहमति वाली स्क्रीन दिखाता है. खाते लिंक करने की सहमति देने के बाद, उपयोगकर्ता को वापस Google के ऐप्लिकेशन पर भेज दिया जाता है.

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

ज़रूरी शर्तें

  1. आपको यह बताना होगा कि उपयोगकर्ता का खाता Google से लिंक किया जाएगा. हालांकि, यह Google Home या Google Assistant जैसे किसी खास Google प्रॉडक्ट से नहीं लिंक किया जाएगा.

सुझाव

हमारा सुझाव है कि आप ये काम करें:

  1. Google की निजता नीति दिखाओ. सहमति वाली स्क्रीन पर, Google की निजता नीति का लिंक शामिल करें.

  2. शेयर किया जाने वाला डेटा. उपयोगकर्ता को साफ़ तौर पर और कम शब्दों में बताएं कि Google को उसका कौनसा डेटा चाहिए और क्यों चाहिए.

  3. साफ़ तौर पर कॉल-टू-ऐक्शन दिया गया हो. सहमति लेने के लिए दिखाई जाने वाली स्क्रीन पर, कॉल-टू-ऐक्शन साफ़ तौर पर बताएं. जैसे, “सहमति दें और लिंक करें.” ऐसा इसलिए, क्योंकि उपयोगकर्ताओं को यह पता होना चाहिए कि खातों को लिंक करने के लिए, उन्हें Google के साथ कौनसा डेटा शेयर करना होगा.

  4. सदस्यता रद्द करने की सुविधा. अगर उपयोगकर्ता खाता लिंक नहीं करना चाहते हैं, तो उन्हें वापस जाने या रद्द करने का विकल्प दें.

  5. साइन इन करने की आसान प्रोसेस. पक्का करें कि उपयोगकर्ताओं के पास अपने Google खाते में साइन इन करने का आसान तरीका हो. जैसे, उपयोगकर्ता नाम और पासवर्ड के लिए फ़ील्ड या Google से साइन इन करें.

  6. अनलिंक करने की सुविधा. उपयोगकर्ताओं को खाता अनलिंक करने का तरीका बताएं. जैसे, आपके प्लैटफ़ॉर्म पर मौजूद खाते की सेटिंग का यूआरएल. इसके अलावा, Google खाते का लिंक शामिल किया जा सकता है. इससे उपयोगकर्ता, लिंक किए गए खाते को मैनेज कर सकते हैं.

  7. उपयोगकर्ता खाते को बदलने की सुविधा. उपयोगकर्ताओं को उनके खाते स्विच करने का तरीका सुझाएं. यह खास तौर पर तब फ़ायदेमंद होता है, जब उपयोगकर्ताओं के पास एक से ज़्यादा खाते हों.

    • अगर किसी उपयोगकर्ता को खाता बदलने के लिए, सहमति वाली स्क्रीन बंद करनी पड़ती है, तो Google को ऐसी गड़बड़ी की जानकारी भेजें जिसे ठीक किया जा सके. इससे उपयोगकर्ता, OAuth लिंकिंग और इंप्लिसिट फ़्लो का इस्तेमाल करके, अपने पसंदीदा खाते में साइन इन कर पाएगा.
  8. अपना लोगो शामिल करें. सहमति वाली स्क्रीन पर, अपनी कंपनी का लोगो दिखाएं. लोगो को सही जगह पर रखने के लिए, स्टाइल से जुड़े दिशा-निर्देशों का इस्तेमाल करें. अगर आपको Google का लोगो भी दिखाना है, तो लोगो और ट्रेडमार्क देखें.

प्रोजेक्ट बनाना

खाता जोड़ने की सुविधा का इस्तेमाल करने के लिए, अपना प्रोजेक्ट बनाने के लिए:

  1. Google API Console पर जाएं.
  2. प्रोजेक्ट बनाएं पर क्लिक करें.
  3. कोई नाम डालें या जनरेट किए गए सुझाव को स्वीकार करें.
  4. बाकी बचे फ़ील्ड की पुष्टि करें या उनमें बदलाव करें.
  5. बनाएं पर क्लिक करें.

अपना प्रोजेक्ट आईडी देखने के लिए:

  1. Google API Console पर जाएं.
  2. लैंडिंग पेज पर मौजूद टेबल में, अपना प्रोजेक्ट ढूंढें. प्रोजेक्ट आईडी, आईडी कॉलम में दिखता है.

Google खाता जोड़ने की प्रोसेस में, सहमति वाली स्क्रीन शामिल होती है. इस स्क्रीन पर उपयोगकर्ताओं को यह जानकारी मिलती है कि कौनसे ऐप्लिकेशन उनके डेटा को ऐक्सेस करने का अनुरोध कर रहे हैं, वे किस तरह का डेटा मांग रहे हैं, और उन पर कौनसी शर्तें लागू होती हैं. Google API क्लाइंट आईडी जनरेट करने से पहले, आपको उस स्क्रीन को कॉन्फ़िगर करना होगा जहां OAuth के लिए सहमति दी जाती है.

  1. Google APIs कंसोल के, उस स्क्रीन वाले पेज को खोलें जहां OAuth के लिए सहमति दी जाती है.
  2. अगर आपसे पूछा जाए, तो वह प्रोजेक्ट चुनें जिसे आपने अभी बनाया है.
  3. "OAuth के लिए सहमति वाली स्क्रीन" पेज पर, फ़ॉर्म भरें और “सेव करें” बटन पर क्लिक करें.

    ऐप्लिकेशन का नाम: सहमति का अनुरोध करने वाले ऐप्लिकेशन का नाम. नाम से आपके ऐप्लिकेशन की सही जानकारी मिलनी चाहिए. साथ ही, यह नाम उपयोगकर्ताओं को अन्य जगहों पर दिखने वाले ऐप्लिकेशन के नाम से मेल खाना चाहिए. ऐप्लिकेशन का नाम, खाता जोड़ने के लिए सहमति वाली स्क्रीन पर दिखेगा.

    ऐप्लिकेशन का लोगो: सहमति वाली स्क्रीन पर दिखने वाली इमेज. इससे उपयोगकर्ताओं को आपके ऐप्लिकेशन को पहचानने में मदद मिलेगी. लोगो, खाता जोड़ने के लिए सहमति वाली स्क्रीन और खाते की सेटिंग में दिखता है

    सहायता के लिए ईमेल पता: ताकि उपयोगकर्ता, सहमति से जुड़े सवालों के लिए आपसे संपर्क कर सकें.

    Google APIs के लिए स्कोप: स्कोप की मदद से, आपका ऐप्लिकेशन उपयोगकर्ता के निजी Google डेटा को ऐक्सेस कर सकता है. Google खाता जोड़ने के इस्तेमाल के मामले में, डिफ़ॉल्ट स्कोप (ईमेल, प्रोफ़ाइल, openid) काफ़ी है. आपको संवेदनशील स्कोप जोड़ने की ज़रूरत नहीं है. आम तौर पर, स्कोप के लिए अनुरोध, ऐक्सेस की ज़रूरत पड़ने पर ही करना चाहिए. पहले से अनुरोध करके रखना सही नहीं है. ज़्यादा जानें.

    अनुमति वाले डोमेन: Google, आपको और आपके उपयोगकर्ताओं को सुरक्षित रखने के लिए, सिर्फ़ उन ऐप्लिकेशन को अनुमति वाले डोमेन का इस्तेमाल करने की अनुमति देता है जो OAuth का इस्तेमाल करके पुष्टि करते हैं. आपके ऐप्लिकेशन के लिंक, अनुमति वाले डोमेन पर होस्ट किए जाने चाहिए. ज़्यादा जानें.

    ऐप्लिकेशन के होम पेज का लिंक: आपके ऐप्लिकेशन का होम पेज. यह अनुमति वाले डोमेन पर होस्ट किया जाना चाहिए.

    ऐप्लिकेशन की निजता नीति का लिंक: यह Google खाता जोड़ने के लिए सहमति वाली स्क्रीन पर दिखता है. यह अनुमति वाले डोमेन पर होस्ट किया जाना चाहिए.

    ऐप्लिकेशन की सेवा की शर्तों का लिंक (ज़रूरी नहीं): यह अनुमति वाले डोमेन पर होस्ट किया जाना चाहिए.

    पहली इमेज. Tunery नाम के काल्पनिक ऐप्लिकेशन के लिए, Google खाता जोड़ने के लिए सहमति वाली स्क्रीन

  4. "पुष्टि की स्थिति" देखें. अगर आपके ऐप्लिकेशन की पुष्टि की जानी है, तो "पुष्टि के लिए सबमिट करें" बटन पर क्लिक करके, अपने ऐप्लिकेशन को पुष्टि के लिए सबमिट करें. ज़्यादा जानकारी के लिए, OAuth की पुष्टि से जुड़ी ज़रूरी शर्तें देखें.

अपना OAuth सर्वर लागू करना

n

To support the OAuth 2.0 implicit flow, your service makes an authorization endpoint available by HTTPS. This endpoint is responsible for authentication and obtaining consent from users for data access. The authorization endpoint presents a sign-in UI to your users that aren't already signed in and records consent to the requested access.

When a Google application needs to call one of your service's authorized APIs, Google uses this endpoint to get permission from your users to call these APIs on their behalf.

Google Account Linking: OAuth Implicit Flow

The following sequence diagram details interactions between the User, Google, and your service's endpoints.

User Google App / Browser Your Auth Endpoint 1. User initiates linking 2. Redirect to Auth Endpoint (GET) client_id, redirect_uri, state, scope 3. Display Sign-in & Consent Screen 4. User Authenticates & Grants Consent 5. Redirect back to Google with Token (GET) access_token, state 6. Store user tokens 7. Access user resources
Figure 1. The sequence of events in the OAuth 2.0 Implicit flow for Google Account Linking.

Roles and responsibilities

The following table defines the roles and responsibilities of actors in the Google Account Linking (GAL) OAuth Implicit flow. Note that in GAL, Google acts as the OAuth Client, while your service acts as the Identity/Service Provider.

Actor / Component GAL Role Responsibilities
Google App / Server OAuth Client Initiates the flow, receives the access token using a browser redirect, and securely stores it to access your service's APIs.
Your Authorization Endpoint Authorization Server Authenticates your users, obtains their consent, and issues long-lived access tokens directly to Google.
Google Redirect URI Callback Endpoint Receives the user redirect from your authorization service with the access_token and state values in the URL fragment.

A typical OAuth 2.0 implicit flow session initiated by Google has the following flow:

  1. Google opens your authorization endpoint in the user's browser. The user signs in, if not signed in already, and grants Google permission to access their data with your API, if they haven't already granted permission.
  2. Your service creates an access token and returns it to Google. To do so, redirect the user's browser back to Google with the access token attached to the request.
  3. Google calls your service's APIs and attaches the access token with each request. Your service verifies that the access token grants Google authorization to access the API and then completes the API call.

Handle authorization requests

When a Google application needs to perform account linking using an OAuth 2.0 implicit flow, Google sends the user to your authorization endpoint with a request that includes the following parameters:

Authorization endpoint parameters
client_id The client ID you assigned to Google.
redirect_uri The URL to which you send the response to this request.
state A bookkeeping value that is passed back to Google unchanged in the redirect URI.
response_type The type of value to return in the response. For the OAuth 2.0 implicit flow, the response type is always token.
user_locale The Google Account language setting in RFC5646 format used to localize your content in the user's preferred language.

For example, if your authorization endpoint is available at https://myservice.example.com/auth, a request might look like the following:

GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&response_type=token&user_locale=LOCALE

For your authorization endpoint to handle sign-in requests, do the following steps:

  1. Verify the client_id and redirect_uri values to prevent granting access to unintended or misconfigured client apps:

    • Confirm that the client_id matches the client ID you assigned to Google.
    • Confirm that the URL specified by the redirect_uri parameter has the following form:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  2. Check if the user is signed in to your service. If the user isn't signed in, complete your service's sign-in or sign-up flow.

  3. Generate an access token for Google to use to access your API. The access token can be any string value, but it must uniquely represent the user and the client the token is for and must not be guessable.

  4. Send an HTTP response that redirects the user's browser to the URL specified by the redirect_uri parameter. Include all of the following parameters in the URL fragment:

    • access_token: The access token you just generated
    • token_type: The string bearer
    • state: The unmodified state value from the original request

    The following is an example of the resulting URL:

    https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID#access_token=ACCESS_TOKEN&token_type=bearer&state=STATE_STRING

Google's OAuth 2.0 redirect handler receives the access token and confirms that the state value hasn't changed. After Google has obtained an access token for your service, Google attaches the token to subsequent calls to your service APIs.

Handle userinfo requests

The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:

After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.

userinfo endpoint request headers
Authorization header The access token of type Bearer.

For example, if your userinfo endpoint is available at https://myservice.example.com/userinfo, a request might look like the following:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

For your userinfo endpoint to handle requests, do the following steps:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.

लागू करने की पुष्टि करना

OAuth 2.0 Playground टूल का इस्तेमाल करके, लागू करने की पुष्टि की जा सकती है.

टूल में, यह तरीका अपनाएं:

  1. OAuth 2.0 कॉन्फ़िगरेशन विंडो खोलने के लिए, कॉन्फ़िगरेशन पर क्लिक करें.
  2. OAuth फ़्लो फ़ील्ड में, क्लाइंट-साइड चुनें.
  3. OAuth एंडपॉइंट फ़ील्ड में, कस्टम चुनें.
  4. अपने OAuth 2.0 एंडपॉइंट और Google को असाइन किया गया क्लाइंट आईडी, उससे जुड़े फ़ील्ड में डालें.
  5. पहला चरण सेक्शन में, कोई भी Google स्कोप न चुनें. इसके बजाय, इस फ़ील्ड को खाली छोड़ दें या अपने सर्वर के लिए मान्य स्कोप टाइप करें. अगर OAuth स्कोप का इस्तेमाल नहीं किया जाता है, तो कोई भी स्ट्रिंग टाइप करें. जब आपका काम पूरा हो जाए, तो एपीआई को अनुमति दें पर क्लिक करें.
  6. दूसरे चरण और तीसरे चरण सेक्शन में, OAuth 2.0 फ़्लो देखें. साथ ही, यह पुष्टि करें कि हर चरण उम्मीद के मुताबिक काम कर रहा है.

Google खाता लिंक करने की सुविधा का डेमो टूल का इस्तेमाल करके, लागू की गई सुविधा की पुष्टि की जा सकती है.

टूल में, यह तरीका अपनाएं:

  1. Google से साइन इन करें बटन पर क्लिक करें.
  2. वह खाता चुनें जिसे आपको लिंक करना है.
  3. सेवा आईडी डालें.
  4. विकल्प के तौर पर, एक या उससे ज़्यादा ऐसे स्कोप डालें जिनके लिए आपको ऐक्सेस का अनुरोध करना है.
  5. डेमो शुरू करें पर क्लिक करें.
  6. जब कहा जाए, तब पुष्टि करें कि आपके पास खाते को लिंक करने के अनुरोध को स्वीकार या अस्वीकार करने का विकल्प है.
  7. पुष्टि करें कि आपको अपने प्लैटफ़ॉर्म पर रीडायरेक्ट किया गया हो.