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

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

In the implicit flow, Google opens your authorization endpoint in the user's browser. After successful sign in, you return a long-lived access token to Google. This access token is now included in every request sent from Google.

In the authorization code flow, you need two endpoints:

  • The authorization endpoint, which presents the sign-in UI to your users that aren't already signed in. The authorization endpoint also creates a short-lived authorization code to record users' consent to the requested access.

  • The token exchange endpoint, which is responsible for two types of exchanges:

    1. Exchanges an authorization code for a long-lived refresh token and a short-lived access token. This exchange happens when the user goes through the account linking flow.
    2. Exchanges a long-lived refresh token for a short-lived access token. This exchange happens when Google needs a new access token because the one it had expired.

Choose an OAuth 2.0 flow

Although the implicit flow is simpler to implement, Google recommends that access tokens issued by the implicit flow never expire. This is because the user is forced to link their account again after a token expires with the implicit flow. If you need token expiration for security reasons, we strongly recommend that you use the authorization code flow instead.

Design guidelines

This section describes the design requirements and recommendations for the user screen that you host for OAuth linking flows. After it's called by Google's app, your platform displays a sign in to Google page and account linking consent screen to the user. The user is directed back to Google's app after giving their consent to link accounts.

This figure shows the steps for a user to link their Google account
            to your authentication system. The first screenshot shows
            user-initiated linking from your platform. The second image shows
            user sign-in to Google, while the third shows the user consent and
            confirmation for linking their Google account with your app. The
            final screenshot shows a successfully linked user account in the
            Google app.
Figure 1. Account linking user sign in to Google and consent screens.

Requirements

  1. You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.

Recommendations

We recommend that you do the following:

  1. Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.

  2. Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why.

  3. Clear call-to-action. State a clear call-to-action on your consent screen, such as “Agree and link.” This is because users need to understand what data they're required to share with Google to link their accounts.

  4. Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.

  5. Clear sign-in process. Ensure that users have clear method for signing in to their Google account, such as fields for their username and password or Sign in with Google.

  6. Ability to unlink. Offer a mechanism for users to unlink, such as a URL to their account settings on your platform. Alternatively, you can include a link to Google Account where users can manage their linked account.

  7. Ability to change user account. Suggest a method for users to switch their account(s). This is especially beneficial if users tend to have multiple accounts.

    • If a user must close the consent screen to switch accounts, send a recoverable error to Google so the user can sign in to the desired account with OAuth linking and the implicit flow.
  8. Include your logo. Display your company logo on the consent screen. Use your style guidelines to place your logo. If you wish to also display Google's logo, see Logos and trademarks.

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

खाता लिंक करने के मकसद से अपना प्रोजेक्ट बनाने के लिए:

  1. Go to the Google API Console.
  2. प्रोजेक्ट बनाएं पर क्लिक करें
  3. एक नाम दर्ज करें या उत्पन्न सुझाव को स्वीकार करें।
  4. किसी भी शेष फ़ील्ड की पुष्टि करें या संपादित करें।
  5. क्रिएट पर क्लिक करें

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

  1. Go to the Google API Console.
  2. लैंडिंग पृष्ठ पर तालिका में अपना प्रोजेक्ट ढूंढें। प्रोजेक्ट आईडी ID कॉलम में दिखाई देती है।

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

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

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

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

    सहायता ईमेल: इसकी मदद से उपयोगकर्ता, अपनी सहमति के बारे में सवाल पूछने के लिए आपसे संपर्क कर सकते हैं.

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

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

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

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

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

    पहला डायग्राम. काल्पनिक ऐप्लिकेशन और ट्यूरी के लिए, Google खाता लिंक करने की सहमति वाली स्क्रीन

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

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

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.

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 via 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.

उपयोगकर्ता जानकारी अनुरोधों को संभालें

UserInfo endpoint OAuth 2.0 संरक्षित संसाधन है कि लिंक किया गया प्रयोक्ता के बारे में वापसी का दावा है। निम्नलिखित उपयोग के मामलों को छोड़कर, userinfo एंडपॉइंट को लागू करना और होस्ट करना वैकल्पिक है:

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

userinfo समापन बिंदु अनुरोध शीर्षलेख
Authorization header बियरर प्रकार का एक्सेस टोकन।

उदाहरण के लिए, यदि आपके UserInfo समाप्ति बिंदु पर उपलब्ध है https://myservice.example.com/userinfo , एक अनुरोध निम्नलिखित प्रकार दिखाई देंगे:

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

अनुरोधों को संभालने के लिए आपके userinfo समापन बिंदु के लिए, निम्न चरणों का पालन करें:

  1. प्राधिकरण हेडर से एक्सेस टोकन निकालें और एक्सेस टोकन से जुड़े उपयोगकर्ता के लिए जानकारी लौटाएं।
  2. यदि पहुँच टोकन अमान्य है, उपयोग करने के साथ एक HTTP 401 अनधिकृत त्रुटि वापस WWW-Authenticate रिस्पांस हैडर। नीचे एक UserInfo त्रुटि प्रतिसाद का एक उदाहरण है:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    तो एक 401 अनधिकृत, या किसी अन्य असफल त्रुटि प्रतिसाद लिंकिंग प्रक्रिया के दौरान दिया जाता है, त्रुटि गैर वसूली योग्य हो जाएगा, पुनर्प्राप्त टोकन को छोड़ दिया जाएगा और उपयोगकर्ता होगा फिर से जोड़ने की प्रक्रिया शुरू करने के लिए।
  3. पहुँच टोकन मान्य, वापसी और HTTP 200 HTTPS प्रतिक्रिया के शरीर में निम्नलिखित JSON ऑब्जेक्ट के साथ प्रतिक्रिया है:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    
    अपने UserInfo endpoint रिटर्न पर HTTP 200 सफलता प्रतिक्रिया करते हैं, तो टोकन लिया गया और दावों उपयोगकर्ता के Google के खिलाफ पंजीकृत हैं लेखा।

    userinfo समापन बिंदु प्रतिक्रिया
    sub एक विशिष्ट आईडी जो आपके सिस्टम में उपयोगकर्ता की पहचान करती है।
    email उपयोगकर्ता का ईमेल पता।
    given_name वैकल्पिक: उपयोगकर्ता के नाम को।
    family_name वैकल्पिक: उपयोगकर्ता के उपनाम को।
    name वैकल्पिक: उपयोगकर्ता का पूरा नाम।
    picture वैकल्पिक: उपयोगकर्ता के प्रोफ़ाइल चित्र।

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

आप का उपयोग करके अपने कार्यान्वयन को मान्य कर सकते OAuth 2.0 खेल का मैदान उपकरण।

टूल में, निम्न चरणों का पालन करें:

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

आप का उपयोग करके अपने कार्यान्वयन को मान्य कर सकते गूगल खाता लिंक करना डेमो उपकरण।

टूल में, निम्न चरणों का पालन करें:

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