การลิงก์บัญชี Google กับ OAuth

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

OAuth 2.1 และ PKCE สำหรับ Agent

เราขอแนะนำให้บังคับใช้ OAuth 2.1 สำหรับ AI Agent แบบไม่เก็บสถานะและไปป์ไลน์แบบมัลติโมดัล

  • PKCE (Proof Key for Code Exchange): ต้องใช้เพื่อรักษาความปลอดภัย ขั้นตอนรหัสการให้สิทธิ์ ซึ่งจะป้องกันการโจมตีแบบดักรับ
  • ไม่มีขั้นตอนการให้สิทธิ์โดยนัย: ขั้นตอนการให้สิทธิ์โดยนัยจะเปิดเผยโทเค็นเพื่อการเข้าถึงใน URL, ซึ่งเป็นความเสี่ยงด้านความปลอดภัยสำหรับสภาพแวดล้อมของ Agent

บริการของคุณต้องรองรับปลายทาง การให้สิทธิ์ และ การแลกเปลี่ยนโทเค็น ที่เป็นไปตามข้อกำหนด OAuth 2.0/2.1

Create the project

To create your project to use account linking:

  1. Go to the Google API Console.
  2. Click Create project.
  3. Enter a name or accept the generated suggestion.
  4. Confirm or edit any remaining fields.
  5. Click Create.

To view your project ID:

  1. Go to the Google API Console.
  2. Find your project in the table on the landing page. The project ID appears in the ID column.

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 ของโฟลว์ รหัสการให้สิทธิ์ประกอบด้วยปลายทาง 2 รายการ ซึ่งบริการของคุณจะให้บริการผ่าน HTTPS ปลายทางแรกคือปลายทางการให้สิทธิ์ ซึ่งมีหน้าที่ค้นหาหรือขอความยินยอมจากผู้ใช้ในการเข้าถึงข้อมูล ปลายทางการให้สิทธิ์จะแสดง UI การลงชื่อเข้าใช้แก่ผู้ใช้ที่ยังไม่ได้ลงชื่อเข้าใช้ และบันทึกความยินยอมในการเข้าถึงที่ขอ ปลายทางที่ 2 คือปลายทางการแลกเปลี่ยนโทเค็น ซึ่งใช้เพื่อรับสตริงที่เข้ารหัสที่เรียกว่าโทเค็น ซึ่งอนุญาตให้ผู้ใช้เข้าถึงบริการของคุณ

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

การลิงก์บัญชี Google: ขั้นตอนรหัสการให้สิทธิ์ OAuth

แผนภาพลำดับเหตุการณ์ต่อไปนี้แสดงรายละเอียดการโต้ตอบระหว่างผู้ใช้ Google และปลายทางของบริการ

User Google App / Browser Google Server Your Auth Endpoint Your Token Endpoint 1. User initiates linking 2. Redirect to Auth Endpoint (GET) client_id, redirect_uri, state, scope 3. Display Sign-in & Consent Screen 4. User Authenticates & Grants Consent 5. Redirect back to Google (GET) code, state 6. Handle redirect & pass code/state 7. Token Exchange (POST) grant_type=authorization_code, code 8. Return Tokens (200 OK) access_token, refresh_token 9. Store user tokens 10. Access user resources
Figure 1. ลำดับเหตุการณ์ในขั้นตอนรหัสการให้สิทธิ์ OAuth 2.0 สำหรับการลิงก์บัญชี Google

บทบาทและความรับผิดชอบ

ตารางต่อไปนี้กำหนดบทบาทและความรับผิดชอบของผู้มีส่วนร่วมในโฟลว์ OAuth ของการลิงก์บัญชี Google (GAL) โปรดทราบว่าใน GAL นั้น Google จะทำหน้าที่เป็น ไคลเอ็นต์ OAuth ขณะที่บริการของคุณจะทำหน้าที่เป็น ผู้ให้บริการข้อมูลประจำตัว/บริการ

Actor / Component GAL Role Responsibilities
Google App / Server OAuth Client เริ่มต้นโฟลว์ รับรหัสการให้สิทธิ์ แลกเปลี่ยนรหัสเป็น โทเค็น และจัดเก็บโทเค็นอย่างปลอดภัยเพื่อเข้าถึง API ของบริการ
Your Authorization Endpoint Authorization Server ตรวจสอบสิทธิ์ผู้ใช้และขอความยินยอมจากผู้ใช้ในการแชร์สิทธิ์เข้าถึง ข้อมูลกับ Google
Your Token Exchange Endpoint Authorization Server ตรวจสอบรหัสการให้สิทธิ์และโทเค็นการรีเฟรช และออกโทเค็นเพื่อการเข้าถึงให้กับเซิร์ฟเวอร์ของ Google
Google Redirect URI Callback Endpoint รับการเปลี่ยนเส้นทางของผู้ใช้จากบริการการให้สิทธิ์พร้อมค่า code และ state

เซสชันขั้นตอนรหัสการให้สิทธิ์ OAuth 2.0 ที่ Google เริ่มต้นจะมีโฟลว์ดังนี้

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

สูตรการใช้งาน

ทำตามขั้นตอนต่อไปนี้เพื่อใช้โฟลว์รหัสการให้สิทธิ์

ขั้นตอนที่ 1: จัดการคำขอการให้สิทธิ์

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

หากต้องการจัดการคำขอ ให้ทำดังนี้

  1. ตรวจสอบคำขอ:

    • ยืนยันว่า client_id ตรงกับรหัสไคลเอ็นต์ที่กำหนดให้กับ Google
    • ยืนยันว่า redirect_uri ตรงกับ URL การเปลี่ยนเส้นทางที่คาดไว้ของ Google: none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
    • ตรวจสอบว่า response_type คือ code
  2. ตรวจสอบสิทธิ์ผู้ใช้:

    • ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้บริการของคุณหรือไม่
    • หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ ให้แจ้งให้ผู้ใช้ทำตามขั้นตอนการลงชื่อเข้าใช้หรือลงชื่อสมัครใช้
  3. สร้างรหัสการให้สิทธิ์:

    • สร้างรหัสการให้สิทธิ์ที่ไม่ซ้ำกันและคาดเดาได้ยาก ซึ่งเชื่อมโยงกับผู้ใช้และไคลเอ็นต์
    • ตั้งค่าให้รหัสหมดอายุในเวลาประมาณ 10 นาที
  4. เปลี่ยนเส้นทางกลับไปที่ Google:

    • เปลี่ยนเส้นทางเบราว์เซอร์ไปยัง URL ที่ระบุไว้ใน redirect_uri
    • แนบพารามิเตอร์การค้นหาต่อไปนี้
      • code: รหัสการให้สิทธิ์ที่คุณสร้างขึ้น
      • state: ค่าสถานะที่ได้รับจาก Google โดยไม่มีการแก้ไข

ขั้นตอนที่ 2: จัดการคำขอแลกเปลี่ยนโทเค็น

ปลายทางการแลกเปลี่ยนโทเค็นจะประมวลผลคำขอ 2 ประเภท ได้แก่ การแลกรหัสเป็นโทเค็น และการรีเฟรชโทเค็นเพื่อการเข้าถึงที่หมดอายุ โปรดดูข้อตกลงโปรโตคอลโดยละเอียดและข้อกำหนดของพารามิเตอร์ได้ที่ปลายทางการแลกเปลี่ยนโทเค็น

ก. แลกรหัสการให้สิทธิ์เป็นโทเค็น

เมื่อ Google ได้รับรหัสการให้สิทธิ์ ระบบจะเรียกใช้ปลายทางการแลกเปลี่ยนโทเค็น (POST) เพื่อดึงโทเค็น

  1. ตรวจสอบคำขอ:

    • ตรวจสอบ client_id และ client_secret
    • ตรวจสอบว่ารหัสการให้สิทธิ์ถูกต้องและไม่หมดอายุ
    • ยืนยันว่า redirect_uri ตรงกับค่าที่ใช้ในขั้นตอนที่ 1
    • หากการตรวจสอบไม่สำเร็จ ให้ส่งกลับ HTTP 400 Bad Request พร้อม {"error": "invalid_grant"}
  2. ออกโทเค็น:

    • สร้าง refresh_token ที่ใช้ได้นานและ access_token ที่ใช้ได้ในระยะสั้น (โดยปกติคือ 1 ชั่วโมง)
    • ส่งกลับ HTTP 200 OK พร้อมการตอบกลับโทเค็น JSON มาตรฐาน

ข. รีเฟรชโทเค็นเพื่อการเข้าถึง

เมื่อโทเค็นเพื่อการเข้าถึงหมดอายุ Google จะขอโทเค็นใหม่โดยใช้โทเค็นการรีเฟรช

  1. ตรวจสอบคำขอ:

    • ตรวจสอบ client_id, client_secret และ refresh_token
    • หากการตรวจสอบไม่สำเร็จ ให้ส่งกลับ HTTP 400 Bad Request พร้อม {"error": "invalid_grant"}
  2. ออกโทเค็นเพื่อการเข้าถึงใหม่:

    • สร้าง access_token ใหม่ที่ใช้ได้ในระยะสั้น
    • ส่งกลับ HTTP 200 OK พร้อมการตอบกลับโทเค็น JSON (ซึ่งอาจรวมถึงโทเค็นการรีเฟรชใหม่)
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

ในเครื่องมือ ให้ทำตามขั้นตอนต่อไปนี้

  1. คลิกการกำหนดค่า เพื่อเปิดหน้าต่างการกำหนดค่า OAuth 2.0
  2. ในช่องโฟลว์ OAuth ให้เลือกฝั่งไคลเอ็นต์
  3. ในช่องปลายทาง OAuth ให้เลือกกำหนดเอง
  4. ระบุปลายทาง OAuth 2.0 และรหัสไคลเอ็นต์ที่คุณกำหนดให้กับ Google ในช่องที่เกี่ยวข้อง
  5. ในส่วนขั้นตอนที่ 1 ไม่ต้องเลือกขอบเขตของ Google แต่ให้เว้นช่องนี้ว่างไว้หรือพิมพ์ขอบเขตที่ใช้ได้กับเซิร์ฟเวอร์ (หรือสตริงที่กำหนดเองหากคุณไม่ได้ใช้ขอบเขต OAuth) เมื่อเสร็จแล้ว ให้คลิกให้สิทธิ์ API
  6. ในส่วนขั้นตอนที่ 2 และขั้นตอนที่ 3 ให้ทำตามโฟลว์ OAuth 2.0 และตรวจสอบว่าแต่ละขั้นตอนทำงานตามที่ตั้งใจไว้

คุณสามารถตรวจสอบการติดตั้งใช้งานได้โดยใช้เครื่องมือสาธิตการลิงก์บัญชี Google

ในเครื่องมือ ให้ทำตามขั้นตอนต่อไปนี้

  1. คลิกปุ่มลงชื่อเข้าใช้ด้วย Google
  2. เลือกบัญชีที่ต้องการลิงก์
  3. ป้อนรหัสบริการ
  4. ป้อนขอบเขตอย่างน้อย 1 รายการที่คุณจะขอสิทธิ์เข้าถึง (ไม่บังคับ)
  5. คลิกเริ่มการสาธิต
  6. เมื่อได้รับข้อความแจ้ง ให้ยืนยันว่าคุณอาจให้ความยินยอมและปฏิเสธคำขอลิงก์
  7. ยืนยันว่าระบบจะนำคุณไปยังแพลตฟอร์มของคุณ