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.

Create the project

To create your project to use account linking:

The Google Account Linking process includes a consent screen which tells users the application requesting access to their data, what kind of data they are asking for and the terms that apply. You will need to configure your OAuth consent screen before generating a Google API client ID.

  1. Open the OAuth consent screen page of the Google APIs console.
  2. If prompted, select the project you just created.
  3. On the "OAuth consent screen" page, fill out the form and click the “Save” button.

    Application name: The name of the application asking for consent. The name should accurately reflect your application and be consistent with the application name users see elsewhere. The application name will be shown on the Account Linking consent screen.

    Application logo: An image on the consent screen that will help users recognize your app. The logo is shown on Account linking consent screen and on account settings

    Support email: For users to contact you with questions about their consent.

    Scopes for Google APIs: Scopes allow your application to access your user's private Google data. For the Google Account Linking use case, default scope (email, profile, openid) is sufficient, you don’t need to add any sensitive scopes. It is generally a best practice to request scopes incrementally, at the time access is required, rather than up front. Learn more.

    Authorized domains: To protect you and your users, Google only allows applications that authenticate using OAuth to use Authorized Domains. Your applications' links must be hosted on Authorized Domains. Learn more.

    Application Homepage link: Home page for your application. Must be hosted on an Authorized Domain.

    Application Privacy Policy link: Shown on Google Account Linking consent screen. Must be hosted on an Authorized Domain.

    Application Terms of Service link (Optional): Must be hosted on an Authorized Domain.

    Figure 1. Google Account Linking Consent Screen for a fictitious Application, Tunery

  4. Check "Verification Status", if your application needs verification then click the "Submit For Verification" button to submit your application for verification. Refer to OAuth verification requirements for details.

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

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

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

Google के शुरू किए गए किसी आम OAuth 2.0 इंप्लिसिट फ़्लो सेशन में नया फ़्लो:

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

अनुमति देने के अनुरोधों को मैनेज करना

जब किसी Google ऐप्लिकेशन को OAuth 2.0 के ज़रिए खाता लिंक करने की ज़रूरत हो इंप्लिसिट फ़्लो, तो Google उपयोगकर्ता को अनुरोध किया गया है, जिसमें ये पैरामीटर शामिल हैं:

ऑथराइज़ेशन एंडपॉइंट पैरामीटर
client_id वह क्लाइंट आईडी जिसे आपने Google को असाइन किया है.
redirect_uri वह यूआरएल जिस पर आपने इस अनुरोध का जवाब भेजा है.
state हिसाब-किताब की ऐसी वैल्यू जो Google को वापस पास की जाती है. हालांकि, इसमें कोई बदलाव नहीं होता रीडायरेक्ट यूआरआई.
response_type रिस्पॉन्स में दी जाने वाली वैल्यू का टाइप. OAuth 2.0 इंप्लिसिट के लिए फ़्लो के लिए, रिस्पॉन्स का टाइप हमेशा token होता है.
user_locale इस सेटिंग में मौजूद Google खाते की भाषा सेटिंग RFC5646 आपके कॉन्टेंट को उपयोगकर्ता की पसंदीदा भाषा में दिखाने के लिए इस्तेमाल किया जाता है.

उदाहरण के लिए, अगर आपका ऑथराइज़ेशन एंडपॉइंट यहां उपलब्ध है https://myservice.example.com/auth, अनुरोध कुछ ऐसा दिख सकता है:

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

अगर आपको साइन-इन करने के अनुरोधों को मैनेज करना है, तो अपने ऑथराइज़ेशन एंडपॉइंट के लिए ये काम करें चरण:

  1. इसके लिए, client_id और redirect_uri वैल्यू की पुष्टि करें अनचाहे या गलत तरीके से कॉन्फ़िगर किए गए क्लाइंट ऐप्लिकेशन का ऐक्सेस देने से रोकें:

    • पुष्टि करें कि client_id आपके क्लाइंट आईडी से मेल खाता है असाइन किया गया है.
    • पुष्टि करें कि redirect_uri में बताया गया यूआरएल पैरामीटर का यह रूप होता है:
      https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
      https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
      
  2. देखें कि उपयोगकर्ता ने आपकी सेवा में साइन इन किया है या नहीं. अगर उपयोगकर्ता ने साइन इन नहीं किया है में जाएं, तो अपनी सेवा का साइन-इन या साइन-अप फ़्लो पूरा करें.

  3. Google के लिए ऐक्सेस टोकन जनरेट करें, ताकि वह आपके एपीआई को ऐक्सेस कर सके. कॉन्टेंट बनाने ऐक्सेस टोकन कोई भी स्ट्रिंग वैल्यू हो सकता है, लेकिन यह टोकन जिस उपयोगकर्ता और क्लाइंट के लिए है, वह अनुमान लगाने लायक नहीं होना चाहिए.

  4. ऐसा एचटीटीपी रिस्पॉन्स भेजें जो उपयोगकर्ता के ब्राउज़र को यूआरएल पर रीडायरेक्ट करे redirect_uri पैरामीटर से तय होता है. सभी को शामिल करें यूआरएल फ़्रैगमेंट में ये पैरामीटर होना चाहिए:

    • access_token: वह ऐक्सेस टोकन जिसे आपने अभी-अभी जनरेट किया है
    • token_type: स्ट्रिंग bearer
    • state: मूल वैल्यू से, बिना बदलाव वाली स्थिति की वैल्यू अनुरोध

    इससे बनने वाले यूआरएल का एक उदाहरण नीचे दिया गया है:

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

Google के OAuth 2.0 रीडायरेक्ट हैंडलर को ऐक्सेस टोकन मिलता है और वह इसकी पुष्टि करता है state की वैल्यू में कोई बदलाव नहीं हुआ है. Google के पास अगर आपको अपनी सेवा का ऐक्सेस टोकन मिलता है, तो Google इस टोकन को अगले कॉल में अटैच कर देता है. आपके सेवा API में शामिल हो.

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 टूल का इस्तेमाल करके, पुष्टि की जा सकती है कि आपने OAuth 2.0 को सही तरीके से लागू किया है या नहीं.

टूल में, नीचे दिया गया तरीका अपनाएं:

  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. पुष्टि करें कि आपको अपने प्लैटफ़ॉर्म पर रीडायरेक्ट किया गया है.