การลิงก์บัญชี Google กับ OAuth (โฟลว์โดยนัย - เก็บถาวร)

หากต้องการรองรับขั้นตอนการให้สิทธิ์โดยนัยของ OAuth 2.0 บริการของคุณต้องทำให้ปลายทางการให้สิทธิ์พร้อมใช้งานผ่าน HTTPS ปลายทางนี้มีหน้าที่ตรวจสอบสิทธิ์และ ขอความยินยอมจากผู้ใช้สำหรับการเข้าถึงข้อมูล ปลายทางการให้สิทธิ์ จะแสดง UI การลงชื่อเข้าใช้ต่อผู้ใช้ที่ยังไม่ได้ลงชื่อเข้าใช้และบันทึก ความยินยอมในการเข้าถึงที่ขอ

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

การลิงก์บัญชี Google: ขั้นตอน OAuth โดยนัย

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

ผู้ใช้ แอป Google / เบราว์เซอร์ ปลายทาง การตรวจสอบสิทธิ์ 1. ผู้ใช้เริ่มการลิงก์ 2. เปลี่ยนเส้นทางไปยังปลายทางการให้สิทธิ์ (GET) client_id, redirect_uri, state, scope 3. แสดงหน้าจอลงชื่อเข้าใช้และความยินยอม 4. ผู้ใช้ตรวจสอบสิทธิ์และให้ความยินยอม 5. เปลี่ยนเส้นทางกลับไปที่ Google พร้อมโทเค็น (GET) access_token, state 6. โทเค็นผู้ใช้ร้านค้า 7. เข้าถึงแหล่งข้อมูลของผู้ใช้
รูปที่ 1 ลำดับเหตุการณ์ในขั้นตอนการให้สิทธิ์โดยนัย OAuth 2.0 สำหรับการลิงก์บัญชี Google

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

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

ผู้ดำเนินการ / คอมโพเนนต์ บทบาท GAL หน้าที่รับผิดชอบ
แอป / เซิร์ฟเวอร์ของ Google ไคลเอ็นต์ OAuth เริ่มโฟลว์ รับโทเค็นเพื่อการเข้าถึงโดยใช้การเปลี่ยนเส้นทางของเบราว์เซอร์ และจัดเก็บอย่างปลอดภัยเพื่อเข้าถึง API ของบริการ
ปลายทางการให้สิทธิ์ของคุณ เซิร์ฟเวอร์การให้สิทธิ์ ตรวจสอบสิทธิ์ผู้ใช้ ขอรับความยินยอมจากผู้ใช้ และออกโทเค็นเพื่อการเข้าถึงที่มีอายุยาวนาน ให้กับ Google โดยตรง
URI การเปลี่ยนเส้นทางของ Google ปลายทางการเรียกกลับ รับการเปลี่ยนเส้นทางผู้ใช้จากบริการให้สิทธิ์ของคุณโดยมีค่า access_token และ state ใน URL Fragment

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

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

สูตรการติดตั้งใช้งาน

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

ขั้นตอนที่ 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 เป็น token
  2. ตรวจสอบสิทธิ์ผู้ใช้:

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

    • สร้างโทเค็นเพื่อการเข้าถึงที่ไม่ซ้ำกันและคาดเดาไม่ได้ซึ่งเชื่อมโยงกับผู้ใช้และไคลเอ็นต์
  4. เปลี่ยนเส้นทางกลับไปที่ Google

    • เปลี่ยนเส้นทางเบราว์เซอร์ไปยัง URL ที่ระบุใน redirect_uri
    • ผนวกพารามิเตอร์ต่อไปนี้ในส่วนย่อย URL (แฮช)
      • access_token: โทเค็นเพื่อการเข้าถึงที่คุณสร้างขึ้น
      • token_type: ต้องเป็น bearer
      • state: ค่าสถานะที่ได้รับจาก Google โดยไม่มีการแก้ไข
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.