OAuth और 'Google साइन-इन' लिंकिंग टाइप, OAuth के आधार पर Google साइन-इन को सबसे ऊपर जोड़ता है खाता लिंक करने की सुविधा. इससे Google उपयोगकर्ताओं को आसानी से आवाज़ के आधार पर लिंक करने की सुविधा मिलती है साथ ही, आपने अपनी सेवा के लिए रजिस्टर करने वाले लोगों के लिए खाता लिंक करने की सुविधा चालू की है जो Google से बाहर की है.
लिंक करने का यह टाइप 'Google साइन-इन' से शुरू होता है. इससे यह पता लगाया जा सकता है कि उपयोगकर्ता ने Google प्रोफ़ाइल की जानकारी आपके सिस्टम में मौजूद होती है. अगर उपयोगकर्ता की जानकारी जो आपके सिस्टम में नहीं मिला, तो स्टैंडर्ड OAuth फ़्लो शुरू हो जाता है. उपयोगकर्ता यह काम भी कर सकता है: अपनी Google प्रोफ़ाइल की जानकारी का इस्तेमाल करके नया खाता बनाने का विकल्प चुन सकते हैं.
OAuth और 'Google साइन-इन' की मदद से खाता लिंक करने के लिए, यहां दिए गए सामान्य निर्देशों का पालन करें चरण:
- सबसे पहले, उपयोगकर्ता से Google पर अपनी प्रोफ़ाइल को ऐक्सेस करने की सहमति मांगें.
- उपयोगकर्ता की पहचान करने के लिए, उनकी प्रोफ़ाइल में दी गई जानकारी का इस्तेमाल करें.
- अगर आपको अपने पुष्टि करने वाले सिस्टम में, Google उपयोगकर्ता के लिए कोई मैच नहीं मिलता है, तो
आपने Actions प्रोजेक्ट को कॉन्फ़िगर किया है या नहीं, इसके हिसाब से फ़्लो आगे बढ़ता है
का इस्तेमाल करके या सिर्फ़ बोलकर उपयोगकर्ता खाता बनाने के लिए, Actions कंसोल में जाकर
आपकी वेबसाइट पर आते हैं.
- अगर आपने बोलकर खाता बनाने की अनुमति दी है, तो आईडी की पुष्टि करें Google से मिला टोकन. इसके बाद, इस आधार पर उपयोगकर्ता बनाया जा सकता है: आईडी टोकन में मौजूद प्रोफ़ाइल जानकारी.
- अगर आपने बोलकर खाता बनाने की अनुमति नहीं दी है, तो उपयोगकर्ता को ऐसा ब्राउज़र होता है जहां वे आपके अनुमति देने वाले पेज को लोड कर सकते हैं और बनाने की प्रोसेस पर जाएं.
बोलकर खाता बनाने की सुविधा
अगर आप आवाज़ से उपयोगकर्ता खाता बनाने की अनुमति देते हैं, तो Assistant उपयोगकर्ता से पूछती है कि क्या वे नीचे दिए गए काम करना चाहते हैं:
- बच्चे के Google खाते की जानकारी का इस्तेमाल करके, उसे अपने सिस्टम पर नया खाता बनाएं या
- अपने पुष्टि करने वाले सिस्टम में किसी दूसरे खाते से साइन इन करें, अगर उसके पास कोई आपका मौजूदा गैर-Google खाता.
अगर आपको अपनी रिपोर्ट को छोटा करना है, तो बोलकर खाता बनाने की अनुमति देने का सुझाव दिया जाता है खाता बनाने के फ़्लो में आने वाली परेशानी. उपयोगकर्ता को सिर्फ़ वॉइस फ़्लो छोड़ना होगा अगर वे Google से बाहर के किसी मौजूदा खाते से साइन इन करना चाहें.
बोलकर खाता बनाने की अनुमति न दें
अगर आपने आवाज़ का इस्तेमाल करके उपयोगकर्ता खाता बनाने की अनुमति नहीं दी है, तो Assistant वह वेबसाइट जिसे आपने उपयोगकर्ता की पुष्टि करने के लिए उपलब्ध कराया था. अगर इंटरैक्शन हो रहा है बिना स्क्रीन वाले डिवाइस में Assistant, उपयोगकर्ता को किसी फ़ोन पर भेजती है खाता लिंक करने का फ़्लो जारी रखने के लिए.
बनाने की अनुमति तब दी जाती है, जब:
जिन उपयोगकर्ताओं के पास Google से बाहर का खाता है उन्हें नया खाता बनाने की अनुमति नहीं देनी चाहिए उपयोगकर्ता खाते और उन्हें अपने मौजूदा उपयोगकर्ता खातों से लिंक करना है, तो पुष्टि करने की ज़रूरत नहीं है. उदाहरण के लिए, अगर आपने लॉयल्टी प्रोग्राम ऑफ़र किया है, तो यह पक्का करना चाहें कि उपयोगकर्ता को मिलने वाले पॉइंट न मिटें मौजूदा खाता है.
आपके पास खाता बनाने की प्रोसेस का पूरा कंट्रोल होना चाहिए. उदाहरण के लिए, आपके पास ये विकल्प हैं बनाने की अनुमति नहीं दें, अगर आपको इस दौरान उपयोगकर्ता को अपनी सेवा की शर्तें दिखानी हैं, तो खाता बनाना.
OAuth और 'Google साइन-इन' खाता लिंक करना लागू करना
खाते, इंडस्ट्री स्टैंडर्ड OAuth 2.0 फ़्लो से लिंक हैं. Actions on Google, इंप्लिसिट और ऑथराइज़ेशन कोड फ़्लो के साथ काम करता है.
इंप्लिसिट कोड फ़्लो में, Google, उपयोगकर्ता के ब्राउज़र में ऑथराइज़ेशन एंडपॉइंट दिखाता है. साइन इन करने के बाद, Google को लंबे समय तक चलने वाला ऐक्सेस टोकन दिखाया जाता है. यह ऐक्सेस टोकन अब Assistant से आपकी कार्रवाई को किए गए हर अनुरोध में शामिल किया गया है.
ऑथराइज़ेशन कोड फ़्लो में, आपको दो एंडपॉइंट की ज़रूरत होगी:
- ऑथराइज़ेशन एंडपॉइंट, जो उन उपयोगकर्ताओं के साइन-इन यूज़र इंटरफ़ेस (यूआई) को दिखाने के लिए ज़िम्मेदार है जिन्होंने पहले से साइन इन नहीं किया है. साथ ही, उपयोगकर्ता को कुछ समय तक इस्तेमाल किए जाने वाले ऑथराइज़ेशन कोड के तौर पर ऐक्सेस का अनुरोध करने के लिए सहमति लेनी होती है.
- टोकन एक्सचेंज एंडपॉइंट, दो तरह के एक्सचेंज के लिए ज़िम्मेदार है:
- लंबे समय तक चलने वाले रीफ़्रेश टोकन और कुछ समय तक चलने वाले ऐक्सेस टोकन के लिए ऑथराइज़ेशन कोड बदलता है. यह एक्सचेंज तब होता है, जब उपयोगकर्ता, खाता जोड़ने के फ़्लो से गुज़रता है.
- वह कम समय तक चलने वाले ऐक्सेस टोकन के लिए, लंबे समय तक चलने वाले रीफ़्रेश टोकन की अदला-बदली करता है. यह ऐसा तब होता है, जब Google को नए ऐक्सेस टोकन की ज़रूरत होती है, क्योंकि उसकी समयसीमा खत्म हो चुकी होती है.
इंप्लिसिट कोड फ़्लो को लागू करना आसान है. हालांकि, Google का सुझाव है कि इंप्लिसिट फ़्लो का इस्तेमाल करके जारी किए गए ऐक्सेस टोकन की समयसीमा कभी खत्म न हो. ऐसा इसलिए, क्योंकि इंप्लिसिट फ़्लो वाले टोकन एंडपॉइंट का इस्तेमाल करने से, उपयोगकर्ता को अपने खाते को फिर से जोड़ने के लिए मजबूर कर दिया जाता है. अगर आपको सुरक्षा से जुड़ी वजहों से टोकन की समयसीमा खत्म होने की ज़रूरत है, तो आपको इसके बजाय ऑथराइज़ेशन कोड फ़्लो का इस्तेमाल करना चाहिए.
प्रोजेक्ट कॉन्फ़िगर करें
OAuth और 'Google साइन-इन' खाते का इस्तेमाल करने के लिए अपने प्रोजेक्ट को कॉन्फ़िगर करने के लिए लिंक करने के बाद, यह तरीका अपनाएं:
- Actions कंसोल खोलें और वह प्रोजेक्ट चुनें जिसका आप इस्तेमाल करना चाहते हैं.
- डेवलप करें टैब पर क्लिक करें और खाता लिंक करना चुनें.
- खाता लिंक करना के बगल में मौजूद स्विच को चालू करें.
- खाता बनाएं सेक्शन में, हां चुनें.
लिंकिंग प्रकार में, OAuth &चुनें Google साइन इन और इंप्लिसिट.
क्लाइंट की जानकारी में ये काम करें:
- Google को की जाने वाली आपकी कार्रवाइयों के ज़रिए जारी किए गए Client-ID को कोई मान असाइन करें, ताकि तो Google से मिलने वाले अनुरोधों को पूरा नहीं करता है.
- ऑथराइज़ेशन और टोकन एक्सचेंज एंडपॉइंट के लिए, यूआरएल डालें.
सेव करें पर क्लिक करें.
अपना OAuth सर्वर लागू करना
OAuth 2.0 इंप्लिसिट फ़्लो के साथ काम करने के लिए, आपकी सेवा अनुमति देती है एंडपॉइंट, एचटीटीपीएस से उपलब्ध है. यह एंडपॉइंट, पुष्टि करने और डेटा ऐक्सेस करने के लिए उपयोगकर्ताओं की सहमति लेना. ऑथराइज़ेशन एंडपॉइंट यह आपके उन उपयोगकर्ताओं को साइन इन करने का यूज़र इंटरफ़ेस (यूआई) दिखाता है जिन्होंने पहले से साइन इन नहीं किया है. यह यूज़र इंटरफ़ेस (यूआई) दिखाता है. के लिए अनुरोध किया गया है.
जब आपकी सेट की गई कार्रवाई को, सेवा के अनुमति वाले किसी एपीआई को कॉल करने की ज़रूरत होती है, तो Google यह एंडपॉइंट आपके उपयोगकर्ताओं से इन एपीआई को कॉल करने की अनुमति लेता है: की ओर से.
Google के शुरू किए गए किसी आम OAuth 2.0 इंप्लिसिट फ़्लो सेशन में नया फ़्लो:
- Google, उपयोगकर्ता के ब्राउज़र में आपका ऑथराइज़ेशन एंडपॉइंट खोलता है. कॉन्टेंट बनाने अगर उपयोगकर्ता ने पहले से साइन इन नहीं किया हुआ है, तो वह साइन इन करता है और Google को आपके एपीआई के साथ अपना डेटा सबमिट कर सकता है, अगर उसने पहले से अनुमति नहीं दी है.
- आपकी सेवा एक ऐक्सेस टोकन बनाती है और उसे वापस भेजती है Google, उपयोगकर्ता के ब्राउज़र को ऐक्सेस टोकन के साथ Google पर वापस रीडायरेक्ट करता है अनुरोध के साथ अटैच की गई है.
- Google आपकी सेवा के एपीआई को कॉल करता है और ऐक्सेस टोकन को हर अनुरोध के साथ किया जा सकता है. आपकी सेवा इस बात की पुष्टि करती है कि ऐक्सेस टोकन, Google को अनुमति देता है या नहीं की अनुमति देता है और फिर एपीआई कॉल को पूरा करता है.
अनुमति देने के अनुरोधों को मैनेज करना
जब आपके Action को OAuth2 इंप्लिसिट फ़्लो के ज़रिए खाता जोड़ना हो, Google, उपयोगकर्ता को एक अनुरोध के साथ आपके ऑथराइज़ेशन एंडपॉइंट पर भेजता है, जिसमें यह शामिल होता है नीचे दिए पैरामीटर:
| ऑथराइज़ेशन एंडपॉइंट पैरामीटर | |
|---|---|
client_id |
वह क्लाइंट आईडी जिसे आपने Google को असाइन किया है. |
redirect_uri |
वह यूआरएल जिस पर आपने इस अनुरोध का जवाब भेजा है. |
state |
हिसाब-किताब की ऐसी वैल्यू जो Google को वापस पास की जाती है. हालांकि, इसमें कोई बदलाव नहीं होता रीडायरेक्ट यूआरआई. |
response_type |
रिस्पॉन्स में दी जाने वाली वैल्यू का टाइप. OAuth 2.0 इंप्लिसिट के लिए
फ़्लो के लिए, रिस्पॉन्स का टाइप हमेशा token होता है. |
उदाहरण के लिए, अगर आपका ऑथराइज़ेशन एंडपॉइंट 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
आपके ऑथराइज़ेशन एंडपॉइंट से साइन-इन करने के अनुरोध मैनेज किए जा सकें, इसके लिए यह तरीका अपनाएं:
इसके लिए,
client_idऔरredirect_uriवैल्यू की पुष्टि करें अनचाहे या गलत तरीके से कॉन्फ़िगर किए गए क्लाइंट ऐप्लिकेशन का ऐक्सेस देने से रोकें:- पुष्टि करें कि
client_idआपके क्लाइंट आईडी से मेल खाता है असाइन किया गया है. - पुष्टि करें कि
redirect_uriमें बताया गया यूआरएल पैरामीटर का यह रूप होता है: YOUR_PROJECT_ID वह आईडी है जो प्रोजेक्ट सेटिंग पेज पर मौजूद है कार्रवाइयां कंसोल पर क्लिक करें.https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID
- पुष्टि करें कि
देखें कि उपयोगकर्ता ने आपकी सेवा में साइन इन किया है या नहीं. अगर उपयोगकर्ता ने साइन इन नहीं किया है में जाएं, तो अपनी सेवा का साइन-इन या साइन-अप फ़्लो पूरा करें.
ऐक्सेस टोकन जनरेट करें. Google इसका इस्तेमाल करके, आपके एपीआई को ऐक्सेस करेगा. कॉन्टेंट बनाने ऐक्सेस टोकन कोई भी स्ट्रिंग वैल्यू हो सकता है, लेकिन यह टोकन जिस उपयोगकर्ता और क्लाइंट के लिए है, वह अनुमान लगाने लायक नहीं होना चाहिए.
ऐसा एचटीटीपी रिस्पॉन्स भेजें जो उपयोगकर्ता के ब्राउज़र को यूआरएल पर रीडायरेक्ट करे
redirect_uriपैरामीटर से तय होता है. सभी को शामिल करें यूआरएल फ़्रैगमेंट में ये पैरामीटर होना चाहिए:access_token: वह ऐक्सेस टोकन जिसे आपने अभी-अभी जनरेट किया हैtoken_type: स्ट्रिंगbearerstate: मूल वैल्यू से, बिना बदलाव की गई स्थिति की वैल्यू अनुरोध इससे बनने वाले यूआरएल का एक उदाहरण नीचे दिया गया है: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 बाद में किए जाने वाले कॉल के लिए टोकन को अटैच करेगा
AppRequest के हिस्से के तौर पर, आपकी सेट की गई कार्रवाई में जोड़े गए हैं.
Handle automatic linking
After the user gives your Action consent to access their Google profile, Google sends a request that contains a signed assertion of the Google user's identity. The assertion contains information that includes the user's Google Account ID, name, and email address. The token exchange endpoint configured for your project handles that request.
If the corresponding Google account is already present in your authentication system,
your token exchange endpoint returns a token for the user. If the Google account doesn't
match an existing user, your token exchange endpoint returns a user_not_found error.
The request has the following form:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&intent=get&assertion=JWT&consent_code=CONSENT_CODE&scope=SCOPES
Your token exchange endpoint must be able to handle the following parameters:
| Token endpoint parameters | |
|---|---|
grant_type |
The type of token being exchanged. For these requests, this
parameter has the value urn:ietf:params:oauth:grant-type:jwt-bearer. |
intent |
For these requests, the value of this parameter is `get`. |
assertion |
A JSON Web Token (JWT) that provides a signed assertion of the Google user's identity. The JWT contains information that includes the user's Google Account ID, name, and email address. |
consent_code |
Optional: When present, a one-time code that indicates that the user has granted consent for your Action to access the specified scopes. |
scope |
Optional: Any scopes you configured Google to request from users. |
When your token exchange endpoint receives the linking request, it should do the following:
验证和解码 JWT 断言
您可以使用适用于您语言的 JWT 解码库来验证和解码 JWT 断言。 使用 Google 的公钥(适用于 JWK 或 PEM 格式)来验证令牌的 签名。
解码后,JWT 断言如以下示例所示:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
除了验证令牌的签名之外,还要验证断言的颁发者
(iss 字段)为 https://accounts.google.com,且受众群体(aud 字段)
是分配给您的 Action 的客户端 ID。
Check if the Google account is already present in your authentication system
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
subfield, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, the user has already signed up and you can issue an access token.
If neither the Google Account ID nor the email address specified in the assertion
matches a user in your database, the user hasn't signed up yet. In this case, your
token exchange endpoint should reply with a HTTP 401 error, that specifies error=user_not_found,
as in the following example:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"user_not_found",
}
user_not_found error, Google
calls your token exchange endpoint with the value of the intent parameter
set to create and sending an ID token that contains the user's profile information
with the request.
Handle account creation via Google Sign-In
When a user needs to create an account on your service, Google makes a
request to your token exchange endpoint that specifies
intent=create, as in the following example:
POST /token HTTP/1.1 Host: oauth2.example.com Content-Type: application/x-www-form-urlencoded response_type=token&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&scope=SCOPES&intent=create&consent_code=CONSENT_CODE&assertion=JWT[&NEW_ACCOUNT_INFO]
The assertion parameter contains A JSON Web Token (JWT) that provides
a signed assertion of the Google user's identity. The JWT contains information
that includes the user's Google Account ID, name, and email address, which you can use
to create a new account on your service.
To respond to account creation requests, your token exchange endpoint must do the following:
验证和解码 JWT 断言
您可以使用适用于您语言的 JWT 解码库来验证和解码 JWT 断言。 使用 Google 的公钥(适用于 JWK 或 PEM 格式)来验证令牌的 签名。
解码后,JWT 断言如以下示例所示:
{ "sub": 1234567890, // The unique ID of the user's Google Account "iss": "https://accounts.google.com", // The assertion's issuer "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID "iat": 233366400, // Unix timestamp of the assertion's creation time "exp": 233370000, // Unix timestamp of the assertion's expiration time "name": "Jan Jansen", "given_name": "Jan", "family_name": "Jansen", "email": "jan@gmail.com", // If present, the user's email address "locale": "en_US" }
除了验证令牌的签名之外,还要验证断言的颁发者
(iss 字段)为 https://accounts.google.com,且受众群体(aud 字段)
是分配给您的 Action 的客户端 ID。
Validate user information and create new account
Check whether either of the following conditions are true:
- The Google Account ID, found in the assertion's
subfield, is in your user database. - The email address in the assertion matches a user in your user database.
If either condition is true, prompt the user to link their existing account with
their Google Account by responding to the request with an HTTP 401 error, specifying
error=linking_error and the user's email address as the login_hint, as in the
following example:
HTTP/1.1 401 Unauthorized
Content-Type: application/json;charset=UTF-8
{
"error":"linking_error",
"login_hint":"foo@bar.com"
}
If neither condition is true, create a new user account using the information provided in the JWT. New accounts do not typically have a password set. It is recommended that you add Google Sign In to other platforms to enable users to log in via Google across the surfaces of your application. Alternatively, you can email the user a link that starts your password recovery flow to allow the user to set a password for signing in on other platforms.
When the creation is completed, issue an access token and return the values in a JSON object in the body of your HTTPS response, like in the following example:
{ "token_type": "Bearer", "access_token": "ACCESS_TOKEN", "expires_in": SECONDS_TO_EXPIRATION }
पुष्टि करने की प्रोसेस शुरू करना
खाते में साइन इन करने के लिए हेल्पर इंटेंट का इस्तेमाल करें पुष्टि करने की प्रोसेस शुरू करने के लिए.
const app = dialogflow({ // REPLACE THE PLACEHOLDER WITH THE CLIENT_ID OF YOUR ACTIONS PROJECT clientId: CLIENT_ID, }) // Intent that starts the account linking flow. app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("Start Signin") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
const app = actionssdk({ clientId: CLIENT_ID, }) app.intent('Start Signin', conv => { conv.ask(new SignIn('To get your account details')) })
private String clientId = "<your_client_id>"; @ForIntent("actions.intent.TEXT") public ActionResponse text(ActionRequest request) { ResponseBuilder rb = getResponseBuilder(request); return rb.add(new SignIn().setContext("To get your account details")).build(); }
डेटा को ऐक्सेस करने के अनुरोधों को मैनेज करना
अगर Assistant के अनुरोध में कोई ऐक्सेस टोकन मौजूद है, पहले जांच लें कि ऐक्सेस टोकन मान्य है और उसकी समयसीमा खत्म नहीं हुई है. इसके बाद, अपने उपयोगकर्ता खाते का डेटाबेस, टोकन से जुड़े उपयोगकर्ता खाते का होता है.