हर Cloud-to-cloud इंटिग्रेशन में, उपयोगकर्ताओं की पुष्टि करने का तरीका शामिल होना चाहिए.
पुष्टि करने की सुविधा की मदद से, अपने उपयोगकर्ताओं के Google खातों को पुष्टि करने वाले सिस्टम में मौजूद उपयोगकर्ता खातों से लिंक किया जा सकता है. इससे आपको अपने उपयोगकर्ताओं की पहचान करने में मदद मिलती है. ऐसा तब होता है, जब स्मार्ट होम इंटेंट को पूरा करने का अनुरोध मिलता है. Google स्मार्ट होम, सिर्फ़ ऑथराइज़ेशन कोड फ़्लो वाले OAuth के साथ काम करता है.
इस पेज पर, OAuth 2.0 सर्वर को सेट अप करने का तरीका बताया गया है, ताकि यह आपके Cloud-to-cloud इंटिग्रेशन के साथ काम कर सके.
OAuth की मदद से 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:
- 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.
- 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.
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.
Requirements
- You must communicate that the user’s account will be linked to Google, not a specific Google product like Google Home or Google Assistant.
- You must have a Google authorization statement such as "By signing in, you are authorizing Google to control your devices." See the Google Device Control Authorization section of the Google Home Developer Policies.
- You must open the Web OAuth linking page and ensure users have a clear method for signing in to their Google Account, such as fields for their username and password. Don't use the Google Sign-In (GSI) method that enables users to link without being taken to the Web OAuth Linking page. It is a violation of Google policy.
- You must include at least one of the following items in the OAuth linking
page to indicate the integration to which the user is linking:
- Company logo
- Company name
- Integration name
- App icon
Recommendations
We recommend that you do the following:
Display Google's Privacy Policy. Include a link to Google’s Privacy Policy on the consent screen.
Data to be shared. Use clear and concise language to tell the user what data of theirs Google requires and why, and what usage or interaction data may be shared with you by Google.
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.
Ability to cancel. Provide a way for users to go back or cancel, if they choose not to link.
Clear sign-in process. Ensure that users have a clear method for signing in to their Google Account, such as fields for their username and password or Sign in with Google.
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. If a user unlinks from your integration, use
agentUsers.deleteto notify Google of the change.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.
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.
ऑथराइज़ेशन कोड फ़्लो
OAuth 2.0 सर्वर के ऑथराइज़ेशन कोड फ़्लो को लागू करने के लिए, दो एंडपॉइंट की ज़रूरत होती है. आपकी सेवा, इन्हें HTTPS के ज़रिए उपलब्ध कराती है. पहला एंडपॉइंट, ऑथराइज़ेशन एंडपॉइंट है. यह डेटा ऐक्सेस करने के लिए, उपयोगकर्ताओं की सहमति लेने या सहमति का पता लगाने के लिए ज़िम्मेदार होता है. ऑथराइज़ेशन एंडपॉइंट, उन लोगों को साइन-इन यूज़र इंटरफ़ेस (यूआई) दिखाता है जिन्होंने पहले से साइन इन नहीं किया है. साथ ही, यह अनुरोध किए गए ऐक्सेस के लिए सहमति रिकॉर्ड करता है. दूसरा एंडपॉइंट, टोकन एक्सचेंज एंडपॉइंट होता है. इसका इस्तेमाल एन्क्रिप्ट (सुरक्षित) की गई स्ट्रिंग पाने के लिए किया जाता है. इन्हें टोकन कहा जाता है. ये टोकन, किसी उपयोगकर्ता को आपकी सेवा ऐक्सेस करने की अनुमति देते हैं.
जब किसी Google ऐप्लिकेशन को आपकी सेवा के किसी एपीआई को कॉल करने की ज़रूरत होती है, तो Google इन एंडपॉइंट का इस्तेमाल एक साथ करता है. इससे वह आपके उपयोगकर्ताओं से, उनकी ओर से इन एपीआई को कॉल करने की अनुमति ले पाता है.
Google की ओर से शुरू किए गए OAuth 2.0 ऑथराइज़ेशन कोड फ़्लो सेशन का फ़्लो इस तरह होता है:
- Google, उपयोगकर्ता के ब्राउज़र में आपका ऑथराइज़ेशन एंडपॉइंट खोलता है. अगर किसी कार्रवाई के लिए, सिर्फ़ आवाज़ से कंट्रोल होने वाले डिवाइस पर फ़्लो शुरू किया गया है, तो Google उस कार्रवाई को फ़ोन पर ट्रांसफ़र कर देता है.
- अगर उपयोगकर्ता ने पहले से साइन इन नहीं किया है, तो वह साइन इन करता है. इसके बाद, अगर उसने पहले से अनुमति नहीं दी है, तो वह Google को आपके एपीआई की मदद से अपना डेटा ऐक्सेस करने की अनुमति देता है.
- आपकी सेवा, ऑथराइज़ेशन कोड बनाती है और उसे Google को भेजती है. इसके लिए, उपयोगकर्ता के ब्राउज़र को वापस Google पर रीडायरेक्ट करें. साथ ही, अनुरोध से जुड़ा ऑथराइज़ेशन कोड अटैच करें.
- Google, ऑथराइज़ेशन कोड को आपके टोकन एक्सचेंज एंडपॉइंट पर भेजता है. यह कोड की पुष्टि करता है और ऐक्सेस टोकन और रीफ़्रेश टोकन दिखाता है. ऐक्सेस टोकन, कम समय तक चलने वाला टोकन होता है. आपकी सेवा, एपीआई को ऐक्सेस करने के लिए क्रेडेंशियल के तौर पर इसे स्वीकार करती है. रीफ़्रेश टोकन, लंबे समय तक इस्तेमाल किया जा सकने वाला टोकन होता है. Google इसे सेव कर सकता है और इसकी मदद से, ऐक्सेस टोकन की समयसीमा खत्म होने पर नए ऐक्सेस टोकन हासिल कर सकता है.
- उपयोगकर्ता के खाता लिंक करने की प्रोसेस पूरी करने के बाद, Google से भेजे गए हर अनुरोध में ऐक्सेस टोकन शामिल होता है.
अनुमति देने के अनुरोध मैनेज करना
जब आपको OAuth 2.0 ऑथराइज़ेशन कोड फ़्लो का इस्तेमाल करके खाता लिंक करना होता है, तो Google उपयोगकर्ता को आपके ऑथराइज़ेशन एंडपॉइंट पर भेजता है. इस अनुरोध में ये पैरामीटर शामिल होते हैं:
| ऑथराइज़ेशन एंडपॉइंट के पैरामीटर | |
|---|---|
client_id |
Google को असाइन किया गया क्लाइंट आईडी. |
redirect_uri |
वह यूआरएल जिस पर आपको इस अनुरोध का जवाब भेजना है. |
state |
यह हिसाब-किताब की ऐसी वैल्यू होती है जिसे रीडायरेक्ट यूआरआई में बिना बदलाव किए, Google को वापस भेज दिया जाता है. |
scope |
ज़रूरी नहीं: यह स्पेस से अलग किया गया स्कोप स्ट्रिंग का एक सेट होता है. इससे यह पता चलता है कि Google किस डेटा के लिए अनुमति मांग रहा है. |
response_type |
जवाब में किस तरह की वैल्यू दिखानी है. OAuth 2.0 के ऑथराइज़ेशन कोड फ़्लो के लिए, रिस्पॉन्स टाइप हमेशा code होता है.
|
उदाहरण के लिए, अगर आपका ऑथराइज़ेशन एंडपॉइंट https://myservice.example.com/auth पर उपलब्ध है, तो अनुरोध कुछ ऐसा दिख सकता है:
GET https://myservice.example.com/auth?client_id=GOOGLE_CLIENT_ID&redirect_uri=REDIRECT_URI&state=STATE_STRING&scope=REQUESTED_SCOPES&response_type=code
साइन-इन करने के अनुरोधों को मैनेज करने के लिए, अपने अनुमति वाले एंडपॉइंट पर यह तरीका अपनाएं:
- पुष्टि करें कि
client_id, Google को असाइन किए गए क्लाइंट आईडी से मेल खाता हो. साथ ही,redirect_uri, Google की ओर से आपकी सेवा के लिए दिए गए रीडायरेक्ट यूआरएल से मेल खाता हो. ये जांच इसलिए ज़रूरी हैं, ताकि अनचाहे या गलत तरीके से कॉन्फ़िगर किए गए क्लाइंट ऐप्लिकेशन को ऐक्सेस न दिया जाए. अगर आपने एक से ज़्यादा OAuth 2.0 फ़्लो इस्तेमाल किए हैं, तो यह भी पक्का करें किresponse_typecodeहो. - यह कुकी यह पता लगाती है कि उपयोगकर्ता ने आपकी सेवा में साइन इन किया है या नहीं. अगर उपयोगकर्ता ने साइन इन नहीं किया है, तो अपनी सेवा के लिए साइन इन या साइन अप करने की प्रोसेस पूरी करें.
- Google के लिए एक अनुमति कोड जनरेट करें, ताकि वह आपके एपीआई को ऐक्सेस कर सके. अनुमति देने वाला कोड, स्ट्रिंग वैल्यू हो सकता है. हालांकि, यह कोड उपयोगकर्ता, टोकन का इस्तेमाल करने वाले क्लाइंट, और कोड के खत्म होने के समय के बारे में यूनीक जानकारी देता हो. साथ ही, इसका अनुमान नहीं लगाया जा सकता हो. आम तौर पर, आपको अनुमति देने वाले कोड जारी करने होते हैं. ये कोड करीब 10 मिनट बाद काम नहीं करते.
- पुष्टि करें कि
redirect_uriपैरामीटर से तय किए गए यूआरएल का फ़ॉर्मैट यह है:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
- उपयोगकर्ता के ब्राउज़र को
redirect_uriपैरामीटर से तय किए गए यूआरएल पर रीडायरेक्ट करें.codeऔरstateपैरामीटर जोड़कर रीडायरेक्ट करते समय, अभी जनरेट किया गया अनुमति कोड और बिना बदलाव वाली ओरिजनल स्थिति की वैल्यू शामिल करें. यहां नतीजे के तौर पर मिले यूआरएल का एक उदाहरण दिया गया है:https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID?code=AUTHORIZATION_CODE&state=STATE_STRING
टोकन एक्सचेंज करने के अनुरोधों को मैनेज करना
आपकी सेवा का टोकन एक्सचेंज एंडपॉइंट, दो तरह के टोकन एक्सचेंज के लिए ज़िम्मेदार होता है:
- ऐक्सेस टोकन और रीफ़्रेश टोकन के लिए ऑथराइज़ेशन कोड बदलना
- रीफ़्रेश टोकन के बदले ऐक्सेस टोकन पाना
टोकन एक्सचेंज के अनुरोधों में ये पैरामीटर शामिल होते हैं:
| टोकन एक्सचेंज एंडपॉइंट के पैरामीटर | |
|---|---|
client_id |
यह स्ट्रिंग, अनुरोध के सोर्स की पहचान Google के तौर पर करती है. इस स्ट्रिंग को आपके सिस्टम में, Google के यूनीक आइडेंटिफ़ायर के तौर पर रजिस्टर किया जाना चाहिए. |
client_secret |
ऐसी सीक्रेट स्ट्रिंग जिसे आपने अपनी सेवा के लिए, Google के साथ रजिस्टर किया है. |
grant_type |
एक्सचेंज किया जा रहा टोकन किस तरह का है. यह authorization_code या refresh_token में से कोई एक होता है. |
code |
जब grant_type=authorization_code होता है, तब यह पैरामीटर वह कोड होता है जो Google को आपके साइन-इन या टोकन एक्सचेंज एंडपॉइंट से मिला होता है. |
redirect_uri |
जब grant_type=authorization_code हो, तब यह पैरामीटर, अनुमति के शुरुआती अनुरोध में इस्तेमाल किया गया यूआरएल होता है. |
refresh_token |
grant_type=refresh_token होने पर, यह पैरामीटर वह रीफ़्रेश टोकन होता है जो Google को आपके टोकन एक्सचेंज एंडपॉइंट से मिला था. |
कॉन्फ़िगर करें कि Google, आपके सर्वर पर क्रेडेंशियल कैसे भेजता है
इसे लागू करने के तरीके के आधार पर, आपका अनुमति देने वाला सर्वर, अनुरोध के मुख्य हिस्से या अनुरोध के हेडर में क्लाइंट क्रेडेंशियल पाने की उम्मीद करता है.
डिफ़ॉल्ट रूप से, Google अनुरोध के मुख्य हिस्से में क्रेडेंशियल भेजता है. अगर आपके अनुमति सर्वर को अनुरोध हेडर में क्लाइंट क्रेडेंशियल की ज़रूरत है, तो आपको अपने Cloud-to-cloud इंटिग्रेशन को इसके मुताबिक कॉन्फ़िगर करना होगा:
प्रोजेक्ट की सूची में, उस प्रोजेक्ट के बगल में मौजूद खोलें पर क्लिक करें जिस पर आपको काम करना है.
क्लाउड-टू-क्लाउड में जाकर, डेवलप करें चुनें.
अपने इंटिग्रेशन के बगल में मौजूद, खोलें पर क्लिक करें.
नीचे स्क्रोल करके अनुमतियां (ज़रूरी नहीं) सेक्शन पर जाएं. इसके बाद, Google को एचटीटीपी बेसिक ऑथ हेडर के ज़रिए Client-ID और सीक्रेट ट्रांसमिट करने की अनुमति दें चेकबॉक्स को चुनें.
अपने बदलावों को सेव करने के लिए, सेव करें पर क्लिक करें.
ऐक्सेस टोकन और रीफ़्रेश टोकन के लिए ऑथराइज़ेशन कोड बदलना
जब उपयोगकर्ता साइन इन कर लेता है और आपका ऑथराइज़ेशन एंडपॉइंट, Google को कुछ समय के लिए इस्तेमाल किया जा सकने वाला ऑथराइज़ेशन कोड भेज देता है, तब Google आपके टोकन एक्सचेंज एंडपॉइंट को एक अनुरोध भेजता है. इस अनुरोध में, ऑथराइज़ेशन कोड के बदले ऐक्सेस टोकन और रीफ़्रेश टोकन मांगा जाता है.
इन अनुरोधों के लिए, grant_type की वैल्यू authorization_code होती है. साथ ही, code की वैल्यू, ऑथराइज़ेशन कोड की वह वैल्यू होती है जिसे आपने पहले Google को दिया था. यहां ऑथराइज़ेशन कोड को ऐक्सेस टोकन और रीफ़्रेश टोकन से बदलने के अनुरोध का उदाहरण दिया गया है:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI
ऑथराइज़ेशन कोड के बदले ऐक्सेस टोकन और रीफ़्रेश टोकन पाने के लिए, आपका टोकन एक्सचेंज एंडपॉइंट, POST अनुरोधों का जवाब देता है. इसके लिए, वह यह तरीका अपनाता है:
- पुष्टि करें कि
client_id, अनुरोध के ऑरिजिन को अनुमति वाले ऑरिजिन के तौर पर पहचानता हो. साथ ही,client_secretकी वैल्यू, अनुमानित वैल्यू से मेल खाती हो. - पुष्टि करें कि ऑथराइज़ेशन कोड मान्य हो और उसकी समयसीमा खत्म न हुई हो. साथ ही, अनुरोध में दिया गया क्लाइंट आईडी, ऑथराइज़ेशन कोड से जुड़े क्लाइंट आईडी से मेल खाता हो.
- पुष्टि करें कि
redirect_uriपैरामीटर से तय किया गया यूआरएल, पुष्टि के शुरुआती अनुरोध में इस्तेमाल की गई वैल्यू के जैसा ही है. - अगर ऊपर दी गई सभी शर्तों की पुष्टि नहीं की जा सकती, तो एचटीटीपी 400 अमान्य अनुरोध वाली गड़बड़ी दिखाएं. साथ ही, मुख्य हिस्से के तौर पर
{"error": "invalid_grant"}दिखाएं. - इसके अलावा, ऑथराइज़ेशन कोड से मिले उपयोगकर्ता आईडी का इस्तेमाल करके, रीफ़्रेश टोकन और ऐक्सेस टोकन जनरेट करें. ये टोकन, स्ट्रिंग वैल्यू हो सकते हैं. हालांकि, ये टोकन उस उपयोगकर्ता और क्लाइंट को खास तौर पर दिखाने चाहिए जिसके लिए टोकन बनाया गया है. साथ ही, ये टोकन ऐसे होने चाहिए जिनका अनुमान न लगाया जा सके. ऐक्सेस टोकन के लिए, टोकन के खत्म होने का समय भी रिकॉर्ड करें. आम तौर पर, टोकन जारी करने के एक घंटे बाद यह समय खत्म हो जाता है. रीफ़्रेश टोकन की समयसीमा खत्म नहीं होती.
- एचटीटीपीएस रिस्पॉन्स के मुख्य हिस्से में, यह JSON ऑब्जेक्ट दिखाएं:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "refresh_token": "REFRESH_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
Google, उपयोगकर्ता के लिए ऐक्सेस टोकन और रीफ़्रेश टोकन को सेव करता है. साथ ही, ऐक्सेस टोकन की समयसीमा खत्म होने की जानकारी रिकॉर्ड करता है. ऐक्सेस टोकन की समयसीमा खत्म होने पर, Google आपके टोकन एक्सचेंज एंडपॉइंट से नया ऐक्सेस टोकन पाने के लिए, रीफ़्रेश टोकन का इस्तेमाल करता है.
रीफ़्रेश टोकन के बदले ऐक्सेस टोकन पाना
जब ऐक्सेस टोकन की समयसीमा खत्म हो जाती है, तो Google आपके टोकन एक्सचेंज एंडपॉइंट को एक अनुरोध भेजता है. इस अनुरोध में, रीफ़्रेश टोकन के बदले नया ऐक्सेस टोकन पाने के लिए कहा जाता है.
इन अनुरोधों के लिए, grant_type की वैल्यू refresh_token होती है. साथ ही, refresh_token की वैल्यू, उस रीफ़्रेश टोकन की वैल्यू होती है जिसे आपने पहले Google को दिया था. यहां रीफ़्रेश टोकन को ऐक्सेस टोकन से बदलने के अनुरोध का एक उदाहरण दिया गया है:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded client_id=GOOGLE_CLIENT_ID&client_secret=GOOGLE_CLIENT_SECRET&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
रीफ़्रेश टोकन को ऐक्सेस टोकन से बदलने के लिए, टोकन एक्सचेंज एंडपॉइंट, POST अनुरोधों का जवाब देता है. इसके लिए, वह यह तरीका अपनाता है:
- पुष्टि करें कि
client_idसे यह पता चलता हो कि अनुरोध Google से किया गया है. साथ ही, यह भी पुष्टि करें किclient_secretकी वैल्यू, उम्मीद के मुताबिक हो. - पुष्टि करें कि रीफ़्रेश टोकन मान्य है और अनुरोध में दिया गया क्लाइंट आईडी, रीफ़्रेश टोकन से जुड़े क्लाइंट आईडी से मेल खाता है.
- अगर ऊपर दी गई सभी शर्तों की पुष्टि नहीं की जा सकती, तो एचटीटीपी 400
खराब अनुरोध वाली गड़बड़ी दिखाएं. साथ ही, मुख्य हिस्से के तौर पर
{"error": "invalid_grant"}दिखाएं. - अगर ऐसा नहीं है, तो ऐक्सेस टोकन जनरेट करने के लिए, रीफ़्रेश टोकन से मिले उपयोगकर्ता आईडी का इस्तेमाल करें. ये टोकन, स्ट्रिंग वैल्यू हो सकते हैं. हालांकि, ये उस उपयोगकर्ता और क्लाइंट को खास तौर पर दिखाने चाहिए जिसके लिए टोकन बनाया गया है. साथ ही, इनका अनुमान नहीं लगाया जा सकता. ऐक्सेस टोकन के लिए, टोकन के खत्म होने का समय भी रिकॉर्ड करें. आम तौर पर, टोकन जारी करने के एक घंटे बाद यह समय खत्म हो जाता है.
- HTTPS रिस्पॉन्स के मुख्य हिस्से में, यहां दिया गया JSON ऑब्जेक्ट दिखाएं:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
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:
- Linked Account Sign-In with Google One Tap.
- Frictionless subscription on AndroidTV.
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:
- Extract access token from the Authorization header and return information for the user associated with the access token.
- If the access token is invalid, return an HTTP 401 Unauthorized error with using the
WWW-AuthenticateResponse Header. Below is an example of a userinfo error response: 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.HTTP/1.1 401 Unauthorized WWW-Authenticate: error="invalid_token", error_description="The Access Token expired"
If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:
If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.{ "sub": "USER_UUID", "email": "EMAIL_ADDRESS", "given_name": "FIRST_NAME", "family_name": "LAST_NAME", "name": "FULL_NAME", "picture": "PROFILE_PICTURE", }userinfo endpoint response subA unique ID that identifies the user in your system. emailEmail address of the user. given_nameOptional: First name of the user. family_nameOptional: Last name of the user. nameOptional: Full name of the user. pictureOptional: Profile picture of the user.