การลิงก์บัญชีด้วย Google Sign-In ที่ใช้ OAuth "ปรับปรุง"

ประเภทการลิงก์ "คล่องตัว" ของ Google Sign-In ที่ใช้ OAuth จะเพิ่ม Google Sign-In นอกเหนือจากการลิงก์บัญชีที่ใช้ OAuth ซึ่งจะช่วยให้ผู้ใช้ Google ลิงก์ด้วยเสียงได้อย่างราบรื่น และยังช่วยให้ผู้ใช้ที่ลงทะเบียนบริการของคุณด้วยข้อมูลประจำตัวที่ไม่ใช่ของ Google สามารถลิงก์บัญชีได้ด้วย

การลิงก์ประเภทนี้เริ่มต้นด้วยการลงชื่อเข้าใช้ด้วย Google ซึ่งช่วยให้คุณตรวจสอบได้ว่ามีข้อมูลโปรไฟล์ Google ของผู้ใช้ในระบบหรือไม่ หากไม่พบข้อมูลของผู้ใช้ในระบบของคุณ ระบบจะเริ่มขั้นตอน OAuth มาตรฐาน นอกจากนี้ ผู้ใช้ยังเลือกสร้างบัญชีใหม่โดยใช้ข้อมูลโปรไฟล์ Google ได้ด้วย

รูปที่ 1: หลังจากที่ Action ได้รับสิทธิ์เข้าถึงโปรไฟล์ Google ของผู้ใช้ แล้ว คุณจะใช้โปรไฟล์ดังกล่าวเพื่อค้นหาข้อมูลที่ตรงกันของผู้ใช้ในระบบการตรวจสอบสิทธิ์ได้

หากต้องการลิงก์บัญชีด้วยการลิงก์ประเภทที่ปรับปรุงแล้ว ให้ทำตามขั้นตอนทั่วไปต่อไปนี้

  1. ก่อนอื่น ให้ขอความยินยอมจากผู้ใช้เพื่อเข้าถึงโปรไฟล์ Google ของผู้ใช้
  2. ใช้ข้อมูลในโปรไฟล์เพื่อระบุตัวผู้ใช้
  3. หากไม่พบผู้ใช้ Google ที่ตรงกันในระบบการตรวจสอบสิทธิ์ ขั้นตอนจะดำเนินต่อโดยขึ้นอยู่กับว่าคุณได้กำหนดค่าโปรเจ็กต์ Actions ใน Actions Console เพื่ออนุญาตให้สร้างบัญชีผู้ใช้ผ่านเสียงหรือเฉพาะใน เว็บไซต์ของคุณหรือไม่
    • หากอนุญาตให้สร้างบัญชีผ่านเสียง ให้ตรวจสอบโทเค็น รหัสที่ได้รับจาก Google จากนั้นคุณจะสร้างผู้ใช้ตาม ข้อมูลโปรไฟล์ที่มีอยู่ในโทเค็นรหัสได้
    • หากคุณไม่อนุญาตให้สร้างบัญชีผ่านเสียง ระบบจะโอนผู้ใช้ไปยัง เบราว์เซอร์ที่ผู้ใช้สามารถโหลดหน้าการให้สิทธิ์และทําตามขั้นตอนการสร้าง ผู้ใช้ให้เสร็จสมบูรณ์
หากอนุญาตให้สร้างบัญชีผ่านเสียงและไม่พบโปรไฟล์ Google ที่ตรงกันในระบบการตรวจสอบสิทธิ์ คุณจะต้องตรวจสอบโทเค็นรหัสที่ได้รับจาก Google จากนั้นคุณจะสร้าง
            ผู้ใช้ตามข้อมูลโปรไฟล์ที่มีอยู่ในโทเค็นรหัสได้
            หากคุณไม่อนุญาตให้สร้างบัญชีผู้ใช้ผ่านเสียง ระบบจะ
            โอนผู้ใช้ไปยังเบราว์เซอร์ที่ผู้ใช้สามารถโหลดหน้าการให้สิทธิ์
            และทําตามขั้นตอนให้เสร็จสมบูรณ์
รูปที่ 2 ภาพแสดงโฟลว์ OAuth และการลงชื่อเข้าใช้ Google เมื่อไม่พบข้อมูลของผู้ใช้ในระบบ

รองรับการสร้างบัญชีผ่านเสียง

หากคุณอนุญาตให้สร้างบัญชีผู้ใช้ผ่านเสียง Assistant จะถามผู้ใช้ว่าต้องการทำสิ่งต่อไปนี้หรือไม่

  • สร้างบัญชีใหม่ในระบบโดยใช้ข้อมูลบัญชี Google ของผู้ใช้ หรือ
  • ลงชื่อเข้าใช้ระบบการตรวจสอบสิทธิ์ด้วยบัญชีอื่นหากมีบัญชีที่ไม่ใช่ของ Google อยู่แล้ว

เราขอแนะนำให้เปิดใช้การสร้างบัญชีผ่านเสียงหากคุณต้องการลด ความซับซ้อนของขั้นตอนการสร้างบัญชี ผู้ใช้จะต้องออกจากขั้นตอนการสนทนาด้วยเสียง ก็ต่อเมื่อต้องการลงชื่อเข้าใช้โดยใช้บัญชีที่ไม่ใช่ของ Google ที่มีอยู่

ไม่อนุญาตให้สร้างบัญชีผ่านเสียง

หากคุณไม่อนุญาตให้สร้างบัญชีผู้ใช้ผ่านเสียง Assistant จะเปิด URL ไปยัง เว็บไซต์ที่คุณระบุไว้สำหรับการตรวจสอบสิทธิ์ผู้ใช้ หากการโต้ตอบเกิดขึ้นในอุปกรณ์ที่ไม่มีหน้าจอ Assistant จะนำผู้ใช้ไปยังโทรศัพท์เพื่อดำเนินการขั้นตอนการลิงก์บัญชีต่อ

เราขอแนะนำให้ไม่อนุญาตการสร้างในกรณีต่อไปนี้

  • คุณไม่ต้องการอนุญาตให้ผู้ใช้ที่มีบัญชีที่ไม่ใช่ของ Google สร้างบัญชีผู้ใช้ใหม่ และต้องการให้ผู้ใช้ลิงก์กับบัญชีผู้ใช้ที่มีอยู่ในระบบการตรวจสอบสิทธิ์ของคุณแทน ตัวอย่างเช่น หากคุณมีโปรแกรมสะสมคะแนน คุณอาจต้องตรวจสอบว่าผู้ใช้จะไม่สูญเสียคะแนนที่สะสมไว้ในบัญชีที่มีอยู่

  • คุณต้องควบคุมขั้นตอนการสร้างบัญชีได้อย่างเต็มที่ เช่น คุณอาจ ไม่อนุญาตให้สร้างหากต้องการแสดงข้อกำหนดในการให้บริการแก่ผู้ใช้ในระหว่าง การสร้างบัญชี

ใช้การลิงก์ "สตรีมไลน์" ของ Google Sign-In ที่ใช้ OAuth

บัญชีจะลิงก์กับขั้นตอน OAuth 2.0 ซึ่งเป็นมาตรฐานอุตสาหกรรม Actions on Google รองรับขั้นตอนโดยนัยและขั้นตอนรหัสการให้สิทธิ์

เพื่อให้การตรวจสอบเป็นไปอย่างราบรื่น

In the implicit code 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 the Assistant to your Action.

In the authorization code flow, you need two endpoints:

  • The authorization endpoint, which is responsible for presenting the sign-in UI to your users that aren't already signed in and recording consent to the requested access in the form of a short-lived authorization code.
  • 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.

Although the implicit code flow is simpler to implement, Google recommends that access tokens issued using the implicit flow never expire, because using token expiration with the implicit flow forces the user to link their account again. If you need token expiration for security reasons, you should strongly consider using the auth code flow instead.

กำหนดค่าโปรเจ็กต์

หากต้องการกำหนดค่าโปรเจ็กต์ให้ใช้การลิงก์ที่ปรับปรุงแล้ว ให้ทำตามขั้นตอนต่อไปนี้

  1. เปิดคอนโซล Actions แล้วเลือกโปรเจ็กต์ที่ต้องการใช้
  2. คลิกแท็บพัฒนา แล้วเลือกการลิงก์บัญชี
  3. เปิดสวิตช์ข้างการลิงก์บัญชี
  4. ในส่วนการสร้างบัญชี ให้เลือกใช่

  5. ในประเภทการลิงก์ ให้เลือก OAuth และการลงชื่อเข้าใช้ด้วยบัญชี Google และโดยนัย

  6. ในข้อมูลลูกค้า ให้ทำดังนี้

    • กำหนดค่าให้กับรหัสไคลเอ็นต์ที่ Actions ของคุณออกให้กับ Google เพื่อระบุ คำขอที่มาจาก Google
    • แทรก URL สำหรับปลายทางการให้สิทธิ์และการแลกเปลี่ยนโทเค็น
  7. คลิกบันทึก

ติดตั้งใช้งานเซิร์ฟเวอร์ OAuth

To support the OAuth 2.0 implicit flow, your service makes an authorization endpoint available by HTTPS. This endpoint is responsible for authenticating 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 your Action 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 by redirecting 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 your Action 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.

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

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

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
      YOUR_PROJECT_ID is the ID found on the Project settings page of the Actions Console.
  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 that Google will 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 will receive the access token and confirm that the state value hasn't changed. After Google has obtained an access token for your service, Google will attach the token to subsequent calls to your Action as part of the 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 的公钥(适用于 JWKPEM 格式)来验证令牌的 签名。

解码后,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 sub field, 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",
}
When Google receives the 401 error response with a 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 的公钥(适用于 JWKPEM 格式)来验证令牌的 签名。

解码后,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 sub field, 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
}

ออกแบบอินเทอร์เฟซผู้ใช้ด้วยเสียงสำหรับขั้นตอนการตรวจสอบสิทธิ์

ตรวจสอบว่าผู้ใช้ได้รับการยืนยันแล้วหรือไม่ และเริ่มขั้นตอนการลิงก์บัญชี

  1. เปิดโปรเจ็กต์ Actions Builder ใน Actions Console
  2. สร้างฉากใหม่เพื่อเริ่มการลิงก์บัญชีใน Action โดยทำดังนี้
    1. คลิกฉาก
    2. คลิกไอคอนเพิ่ม (+) เพื่อเพิ่มฉากใหม่
  3. ในฉากที่สร้างขึ้นใหม่ ให้คลิกไอคอนเพิ่ม สำหรับเงื่อนไข
  4. เพิ่มเงื่อนไขที่ตรวจสอบว่าผู้ใช้ที่เชื่อมโยงกับการสนทนาเป็นผู้ใช้ที่ยืนยันแล้วหรือไม่ หากการตรวจสอบล้มเหลว การดำเนินการของคุณจะลิงก์บัญชีไม่ได้ ระหว่างการสนทนา และควรกลับไปให้สิทธิ์เข้าถึง ฟังก์ชันที่ไม่ต้องมีการลิงก์บัญชี
    1. ในช่อง Enter new expression ในส่วนเงื่อนไข ให้ป้อนตรรกะต่อไปนี้ user.verificationStatus != "VERIFIED"
    2. ในส่วนการเปลี่ยนฉาก ให้เลือกฉากที่ไม่ต้องลิงก์บัญชีหรือฉากที่เป็นจุดเริ่มต้นของฟังก์ชันการทำงานสำหรับแขกรับเชิญเท่านั้น

  1. คลิกไอคอนเพิ่ม สำหรับเงื่อนไข
  2. เพิ่มเงื่อนไขเพื่อทริกเกอร์โฟลว์การลิงก์บัญชีหากผู้ใช้ไม่มี ข้อมูลประจำตัวที่เชื่อมโยง
    1. ในช่อง Enter new expression ในส่วนเงื่อนไข ให้ป้อนตรรกะต่อไปนี้ user.verificationStatus == "VERIFIED"
    2. ในส่วนการเปลี่ยนฉาก ให้เลือกฉากระบบการลิงก์บัญชี
    3. คลิกบันทึก

หลังจากบันทึกแล้ว ระบบจะเพิ่มฉากระบบการลิงก์บัญชีใหม่ที่ชื่อ <SceneName>_AccountLinking ลงในโปรเจ็กต์

ปรับแต่งฉากการลิงก์บัญชี

  1. ในส่วนฉาก ให้เลือกฉากระบบการลิงก์บัญชี
  2. คลิกส่งพรอมต์ แล้วเพิ่มประโยคสั้นๆ เพื่ออธิบายให้ผู้ใช้ทราบ ว่าทำไมแอ็กชันจึงต้องเข้าถึงข้อมูลประจำตัวของผู้ใช้ (เช่น "เพื่อบันทึกค่ากำหนดของคุณ")
  3. คลิกบันทึก

  1. ในส่วนเงื่อนไข ให้คลิกหากผู้ใช้ลิงก์บัญชีสำเร็จ
  2. กำหนดค่าว่าขั้นตอนควรดำเนินการอย่างไรหากผู้ใช้ตกลงที่จะลิงก์บัญชี เช่น เรียกใช้เว็บฮุกเพื่อประมวลผลตรรกะทางธุรกิจที่กำหนดเองที่จำเป็น และเปลี่ยนกลับไปที่ฉากต้นทาง
  3. คลิกบันทึก

  1. ในส่วนเงื่อนไข ให้คลิกหากผู้ใช้ยกเลิกหรือปิดการลิงก์บัญชี
  2. กำหนดค่าว่าขั้นตอนการทำงานควรเป็นอย่างไรหากผู้ใช้ไม่ยอมรับการลิงก์บัญชี เช่น ส่งข้อความรับทราบและเปลี่ยนเส้นทางไปยังฉาก ที่ให้ฟังก์ชันการทำงานที่ไม่ต้องลิงก์บัญชี
  3. คลิกบันทึก

  1. ในส่วนเงื่อนไข ให้คลิกหากเกิดข้อผิดพลาดของระบบหรือเครือข่าย
  2. กำหนดค่าวิธีที่โฟลว์ควรดำเนินการหากโฟลว์การลิงก์บัญชีดำเนินการไม่สำเร็จเนื่องจากข้อผิดพลาดของระบบหรือเครือข่าย เช่น ส่งข้อความรับทราบและเปลี่ยนเส้นทางไปยังฉาก ที่ให้ฟังก์ชันการทำงานที่ไม่ต้องลิงก์บัญชี
  3. คลิกบันทึก

จัดการคำขอเข้าถึงข้อมูล

หากคำขอของ Assistant มีโทเค็นเพื่อการเข้าถึง ให้ตรวจสอบก่อนว่าโทเค็นเพื่อการเข้าถึงนั้นถูกต้องและไม่หมดอายุ จากนั้นดึงข้อมูลจาก ฐานข้อมูลบัญชีผู้ใช้ของคุณซึ่งเป็นบัญชีผู้ใช้ที่เชื่อมโยงกับโทเค็น