ระบบจะลิงก์บัญชีโดยใช้ขั้นตอน รหัสการให้สิทธิ์ 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:
- Go to the Google API Console.
- Click Create project.
- Enter a name or accept the generated suggestion.
- Confirm or edit any remaining fields.
- Click Create.
To view your project ID:
- Go to the Google API Console.
- Find your project in the table on the landing page. The project ID appears in the ID column.
Configure your OAuth Consent Screen
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.
- Open the OAuth consent screen page of the Google APIs console.
- If prompted, select the project you just created.
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
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 และปลายทางของบริการ
บทบาทและความรับผิดชอบ
ตารางต่อไปนี้กำหนดบทบาทและความรับผิดชอบของผู้มีส่วนร่วมในโฟลว์ 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 เริ่มต้นจะมีโฟลว์ดังนี้
- Google จะเปิดปลายทางการให้สิทธิ์ในเบราว์เซอร์ของผู้ใช้ หากโฟลว์เริ่มต้นในอุปกรณ์ที่ใช้เสียงเท่านั้นสำหรับ Action Google จะโอนการดำเนินการไปยังโทรศัพท์
- ผู้ใช้ลงชื่อเข้าใช้ (หากยังไม่ได้ลงชื่อเข้าใช้) และให้สิทธิ์ Google ในการเข้าถึงข้อมูลด้วย API ของคุณ (หากยังไม่ได้ให้สิทธิ์)
- บริการของคุณจะสร้าง รหัสการให้สิทธิ์และส่งกลับไปยัง Google โดยเปลี่ยนเส้นทางเบราว์เซอร์ของผู้ใช้กลับไปที่ Google พร้อมรหัสการให้สิทธิ์ที่แนบมากับคำขอ
- Google จะส่งรหัสการให้สิทธิ์ไปยังปลายทางการแลกเปลี่ยนโทเค็น ซึ่งจะตรวจสอบความถูกต้องของรหัสและส่งกลับ โทเค็นเพื่อการเข้าถึงและ โทเค็นการรีเฟรช โทเค็นเพื่อการเข้าถึงเป็นโทเค็นที่ใช้ได้ในระยะสั้น ซึ่งบริการของคุณยอมรับเป็นข้อมูลเข้าสู่ระบบเพื่อเข้าถึง API โทเค็นการรีเฟรชเป็นโทเค็นที่ใช้ได้นาน ซึ่ง Google สามารถจัดเก็บและใช้เพื่อรับโทเค็นเพื่อการเข้าถึงใหม่เมื่อโทเค็นเดิมหมดอายุ
- หลังจากที่ผู้ใช้ทำตามขั้นตอนการลิงก์บัญชีเสร็จสมบูรณ์แล้ว คำขอที่ส่งจาก Google ทุกครั้งจะมีโทเค็นเพื่อการเข้าถึง
สูตรการใช้งาน
ทำตามขั้นตอนต่อไปนี้เพื่อใช้โฟลว์รหัสการให้สิทธิ์
ขั้นตอนที่ 1: จัดการคำขอการให้สิทธิ์
เมื่อ Google เริ่มต้นการลิงก์บัญชี ระบบจะเปลี่ยนเส้นทางผู้ใช้ไปยังปลายทางการให้สิทธิ์ โปรดดูข้อตกลงโปรโตคอลโดยละเอียดและข้อกำหนดของพารามิเตอร์ได้ที่ปลายทางการให้สิทธิ์
หากต้องการจัดการคำขอ ให้ทำดังนี้
ตรวจสอบคำขอ:
- ยืนยันว่า
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
- ยืนยันว่า
ตรวจสอบสิทธิ์ผู้ใช้:
- ตรวจสอบว่าผู้ใช้ลงชื่อเข้าใช้บริการของคุณหรือไม่
- หากผู้ใช้ไม่ได้ลงชื่อเข้าใช้ ให้แจ้งให้ผู้ใช้ทำตามขั้นตอนการลงชื่อเข้าใช้หรือลงชื่อสมัครใช้
สร้างรหัสการให้สิทธิ์:
- สร้างรหัสการให้สิทธิ์ที่ไม่ซ้ำกันและคาดเดาได้ยาก ซึ่งเชื่อมโยงกับผู้ใช้และไคลเอ็นต์
- ตั้งค่าให้รหัสหมดอายุในเวลาประมาณ 10 นาที
เปลี่ยนเส้นทางกลับไปที่ Google:
- เปลี่ยนเส้นทางเบราว์เซอร์ไปยัง URL ที่ระบุไว้ใน
redirect_uri - แนบพารามิเตอร์การค้นหาต่อไปนี้
code: รหัสการให้สิทธิ์ที่คุณสร้างขึ้นstate: ค่าสถานะที่ได้รับจาก Google โดยไม่มีการแก้ไข
- เปลี่ยนเส้นทางเบราว์เซอร์ไปยัง URL ที่ระบุไว้ใน
ขั้นตอนที่ 2: จัดการคำขอแลกเปลี่ยนโทเค็น
ปลายทางการแลกเปลี่ยนโทเค็นจะประมวลผลคำขอ 2 ประเภท ได้แก่ การแลกรหัสเป็นโทเค็น และการรีเฟรชโทเค็นเพื่อการเข้าถึงที่หมดอายุ โปรดดูข้อตกลงโปรโตคอลโดยละเอียดและข้อกำหนดของพารามิเตอร์ได้ที่ปลายทางการแลกเปลี่ยนโทเค็น
ก. แลกรหัสการให้สิทธิ์เป็นโทเค็น
เมื่อ Google ได้รับรหัสการให้สิทธิ์ ระบบจะเรียกใช้ปลายทางการแลกเปลี่ยนโทเค็น (POST) เพื่อดึงโทเค็น
ตรวจสอบคำขอ:
- ตรวจสอบ
client_idและclient_secret - ตรวจสอบว่ารหัสการให้สิทธิ์ถูกต้องและไม่หมดอายุ
- ยืนยันว่า
redirect_uriตรงกับค่าที่ใช้ในขั้นตอนที่ 1 - หากการตรวจสอบไม่สำเร็จ ให้ส่งกลับ HTTP
400 Bad Requestพร้อม{"error": "invalid_grant"}
- ตรวจสอบ
ออกโทเค็น:
- สร้าง
refresh_tokenที่ใช้ได้นานและaccess_tokenที่ใช้ได้ในระยะสั้น (โดยปกติคือ 1 ชั่วโมง) - ส่งกลับ HTTP
200 OKพร้อมการตอบกลับโทเค็น JSON มาตรฐาน
- สร้าง
ข. รีเฟรชโทเค็นเพื่อการเข้าถึง
เมื่อโทเค็นเพื่อการเข้าถึงหมดอายุ Google จะขอโทเค็นใหม่โดยใช้โทเค็นการรีเฟรช
ตรวจสอบคำขอ:
- ตรวจสอบ
client_id,client_secretและrefresh_token - หากการตรวจสอบไม่สำเร็จ ให้ส่งกลับ HTTP
400 Bad Requestพร้อม{"error": "invalid_grant"}
- ตรวจสอบ
ออกโทเค็นเพื่อการเข้าถึงใหม่:
- สร้าง
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:
- 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.
การตรวจสอบการติดตั้งใช้งาน
You can validate your implementation by using the OAuth 2.0 Playground tool.
In the tool, do the following steps:
- Click Configuration to open the OAuth 2.0 Configuration window.
- In the OAuth flow field, select Client-side.
- In the OAuth Endpoints field, select Custom.
- Specify your OAuth 2.0 endpoint and the client ID you assigned to Google in the corresponding fields.
- In the Step 1 section, don't select any Google scopes. Instead, leave this field blank or type a scope valid for your server (or an arbitrary string if you don't use OAuth scopes). When you're done, click Authorize APIs.
- In the Step 2 and Step 3 sections, go through the OAuth 2.0 flow and verify that each step works as intended.
You can validate your implementation by using the Google Account Linking Demo tool.
In the tool, do the following steps:
- Click the Sign in with Google button.
- Choose the account you'd like to link.
- Enter the service ID.
- Optionally enter one or more scopes that you will request access for.
- Click Start Demo.
- When prompted, confirm that you may consent and deny the linking request.
- Confirm that you are redirected to your platform.