การลิงก์บัญชี Google ช่วยให้เจ้าของบัญชี Google เชื่อมต่อกับบริการของคุณได้อย่างรวดเร็วและราบรื่นและปลอดภัย รวมทั้งแชร์ข้อมูลกับ Google
การลงชื่อเข้าใช้บัญชีที่ลิงก์จะเปิดใช้การลงชื่อเข้าใช้ด้วย One Tap กับ Google สําหรับผู้ใช้ที่ลิงก์บัญชี Google กับบริการของคุณแล้ว ซึ่งจะช่วยปรับปรุงประสบการณ์ของผู้ใช้เนื่องจากผู้ใช้ลงชื่อเข้าใช้ได้ในคลิกเดียวโดยไม่ต้องป้อนชื่อผู้ใช้และรหัสผ่านอีกครั้ง นอกจากนี้ยังช่วยลดโอกาสที่ผู้ใช้จะสร้างบัญชีที่ซ้ํากันในบริการด้วย
ข้อกำหนด
คุณต้องลงชื่อเข้าใช้ข้อกําหนดต่อไปนี้เพื่อใช้การลงชื่อเข้าใช้บัญชีที่ลิงก์
- คุณใช้การลิงก์บัญชี OAuth ของบัญชี Google ที่รองรับขั้นตอนการให้สิทธิ์ OAuth 2.0 การใช้งาน OAuth ต้องมีปลายทางต่อไปนี้
- ปลายทางการให้สิทธิ์ เพื่อจัดการคําขอการให้สิทธิ์
- ปลายทางโทเค็น เพื่อจัดการคําขอสิทธิ์เข้าถึงและรีเฟรชโทเค็น
- ปลายทางข้อมูลผู้ใช้เพื่อดึงข้อมูลพื้นฐานของบัญชีเกี่ยวกับผู้ใช้ที่ลิงก์ ซึ่งแสดงต่อผู้ใช้ในระหว่างขั้นตอนการลงชื่อเข้าใช้บัญชีที่ลิงก์
- คุณมีแอป Android
วิธีการทำงาน
สิ่งที่ต้องทําก่อน : ก่อนหน้านี้ผู้ใช้ได้ลิงก์บัญชี Google กับบัญชีของตนเองกับบริการของคุณ
- คุณสามารถเลือกแสดงบัญชีที่ลิงก์ระหว่างขั้นตอนการลงชื่อเข้าใช้ด้วย One Tap
- ผู้ใช้จะเห็นข้อความแจ้งให้ลงชื่อเข้าใช้ด้วย One Tap พร้อมตัวเลือกในการลงชื่อเข้าใช้บริการด้วยบัญชีที่ลิงก์ไว้
- หากผู้ใช้เลือกที่จะดําเนินการต่อด้วยบัญชีที่ลิงก์ Google จะส่งคําขอไปยังปลายทางของโทเค็นของคุณเพื่อบันทึกรหัสการให้สิทธิ์ คําขอมีโทเค็นเพื่อการเข้าถึงของผู้ใช้ซึ่งออกโดยบริการและรหัสการให้สิทธิ์ของ Google
- คุณแลกเปลี่ยนรหัสการให้สิทธิ์ของ Google กับโทเค็น Google ID ที่มีข้อมูลเกี่ยวกับบัญชี Google ของผู้ใช้
- นอกจากนี้ แอปยังจะได้รับโทเค็นรหัสเมื่อกระบวนการเสร็จสิ้นและคุณจับคู่รหัสนี้กับตัวระบุผู้ใช้ในโทเค็นรหัสที่เซิร์ฟเวอร์ได้รับเพื่อลงชื่อเข้าใช้แอปของคุณ

ใช้การลงชื่อเข้าใช้บัญชีที่ลิงก์ในแอป Android
หากต้องการรองรับการลงชื่อเข้าใช้บัญชีที่ลิงก์ในแอป Android โปรดทําตามวิธีการในคู่มือการติดตั้ง Android
จัดการคําขอรหัสการให้สิทธิ์จาก Google
Google จะส่งคําขอ POST ไปยังปลายทางของโทเค็นเพื่อบันทึกรหัสการให้สิทธิ์ที่คุณแลกเปลี่ยนสําหรับโทเค็นรหัสของผู้ใช้ คําขอมีโทเค็นเพื่อการเข้าถึงของผู้ใช้และรหัสการให้สิทธิ์ OAuth2 ของ Google
ก่อนบันทึกรหัสการให้สิทธิ์ คุณต้องยืนยันโทเค็นเพื่อการเข้าถึงที่ Google มอบให้คุณ ซึ่ง client_id
ระบุไว้
คำขอ HTTP
ตัวอย่างคำขอ
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
code=GOOGLE_AUTHORIZATION_CODE
&grant_type=urn:ietf:params:oauth:grant-type:reciprocal
&client_id=CLIENT_ID
&client_secret=CLIENT_SECRET
&access_token=ACCESS_TOKEN
ปลายทางของการแลกเปลี่ยนโทเค็นของคุณต้องสามารถจัดการพารามิเตอร์คําขอต่อไปนี้
พารามิเตอร์ปลายทางของโทเค็น | |
---|---|
code |
รหัสการให้สิทธิ์ Google OAuth2 ต้องระบุ |
client_id |
รหัสลูกค้าที่คุณออกให้กับ Google |
client_secret |
รหัสลับไคลเอ็นต์ที่ต้องออกให้ Google |
access_token |
ต้องระบุโทเค็นเพื่อการเข้าถึงที่ออกให้ Google คุณจะใช้วิธีนี้เพื่อดูบริบทของผู้ใช้ |
grant_type |
ต้องระบุต้องตั้งค่าเป็น urn:ietf:params:oauth:grant-type:reciprocal |
ปลายทางของการแลกเปลี่ยนโทเค็นของคุณควรตอบสนองต่อคําขอ POST โดยทําตามขั้นตอนต่อไปนี้
- ยืนยันว่า
access_token
มอบให้กับ Google ที่ระบุโดยclient_id
- ตอบกลับด้วย HTTP 200 (OK) หากคําขอถูกต้องและแลกเปลี่ยนรหัสการตรวจสอบสิทธิ์กับโทเค็น Google ID สําเร็จ หรือรหัสข้อผิดพลาด HTTP หากคําขอไม่ถูกต้อง
การตอบสนองของ HTTP
สำเร็จ
แสดงรหัสสถานะ HTTP 200 OK
ตัวอย่างคําตอบที่สําเร็จ
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{}
ข้อผิดพลาด
ในกรณีที่คําขอ HTTP ไม่ถูกต้อง โปรดตอบกลับด้วยรหัสข้อผิดพลาด HTTP รหัสใดรหัสหนึ่งต่อไปนี้
รหัสสถานะ HTTP | เนื้อหา | คำอธิบาย |
---|---|---|
400 | {"error": "invalid_request"} |
คําขอไม่มีพารามิเตอร์เพื่อให้เซิร์ฟเวอร์ดําเนินการตามคําขอไม่ได้ คําขอนี้อาจกลับมาหากคําขอมีพารามิเตอร์ที่ไม่รองรับหรือทําซ้ําพารามิเตอร์ |
401 | {"error": "invalid_request"} |
การตรวจสอบสิทธิ์ไคลเอ็นต์ล้มเหลว เช่น หากคําขอมีรหัสไคลเอ็นต์หรือข้อมูลลับที่ไม่ถูกต้อง |
401 | {"error": "invalid_token"}
Include "WWW-Authentication: Bearer" auth Challenge ในส่วนหัวการตอบกลับ |
โทเค็นเพื่อการเข้าถึงของพาร์ทเนอร์ไม่ถูกต้อง |
403 | {"error": "insufficient_permission"}
Include "WWW-Authentication: Bearer" auth Challenge ในส่วนหัวการตอบกลับ |
โทเค็นเพื่อการเข้าถึงของพาร์ทเนอร์ไม่มีขอบเขตที่จําเป็นสําหรับ OAuth แบบ Reciprocal |
500 | {"error": "internal_error"} |
ข้อผิดพลาดเกี่ยวกับเซิร์ฟเวอร์ |
การตอบกลับที่มีข้อผิดพลาดควรมีช่องต่อไปนี้
ช่องคําตอบแสดงข้อผิดพลาด | |
---|---|
error |
สตริงแสดงข้อผิดพลาด จําเป็น |
error_description |
คําอธิบายข้อผิดพลาดที่มนุษย์อ่านได้ |
error_uri |
URI ที่มีรายละเอียดเพิ่มเติมเกี่ยวกับข้อผิดพลาด |
ตัวอย่างการตอบสนองข้อผิดพลาด 400
HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"error": "invalid_request",
"error_description": "Request was missing the 'access_token' parameter."
}
รหัสการให้สิทธิ์ของ Exchange สําหรับโทเค็นรหัส
คุณจะต้องแลกเปลี่ยนรหัสการให้สิทธิ์ที่คุณได้รับสําหรับโทเค็น Google ID ซึ่งมีข้อมูลเกี่ยวกับบัญชี Google ของผู้ใช้
หากต้องการแลกเปลี่ยนรหัสการให้สิทธิ์สําหรับโทเค็นรหัส Google ให้เรียกใช้ปลายทาง https://oauth2.googleapis.com/token
และตั้งค่าพารามิเตอร์ต่อไปนี้
ช่องคําขอ | |
---|---|
client_id |
ต้องระบุรหัสไคลเอ็นต์ที่ได้จากหน้าข้อมูลเข้าสู่ระบบของคอนโซล API ซึ่งโดยปกติแล้วจะเป็นข้อมูลเข้าสู่ระบบที่ใช้ชื่อว่าแอปการดําเนินการใหม่บน Google |
client_secret |
ต้องระบุรหัสลับไคลเอ็นต์ที่ได้รับจากหน้าข้อมูลเข้าสู่ระบบของคอนโซล API |
code |
ต้องระบุรหัสการให้สิทธิ์ที่ส่งในคําขอเริ่มต้น |
grant_type |
ต้องระบุ ตามที่กําหนดไว้ในข้อกําหนด OAuth 2.0 ค่าของช่องนี้จะต้องตั้งค่าเป็น authorization_code |
ตัวอย่างคำขอ
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
code=GOOGLE_AUTHORIZATION_CODE
&grant_type=authorization_code
&client_id=GOOGLE_CLIENT_ID
&client_secret=GOOGLE_CLIENT_SECRET
Google จะตอบสนองต่อคําขอนี้โดยการส่งคืนออบเจ็กต์ JSON ที่มีโทเค็นเพื่อการเข้าถึงที่มีอายุสั้นและโทเค็นการรีเฟรช
การตอบสนองประกอบด้วยช่องต่อไปนี้
ช่องคําตอบ | |
---|---|
access_token |
โทเค็นการเข้าถึงที่ออกโดย Google ที่แอปพลิเคชันของคุณส่งเพื่อให้สิทธิ์คําขอ Google API |
id_token |
โทเค็นรหัสมีข้อมูลบัญชี Google ของผู้ใช้ ส่วนการตรวจสอบความถูกต้องของการตอบกลับมีรายละเอียดเกี่ยวกับวิธีถอดรหัสและตรวจสอบการตอบสนองของโทเค็นรหัส |
expires_in |
อายุการใช้งานที่เหลือของโทเค็นเพื่อการเข้าถึงเป็นวินาที |
refresh_token |
โทเค็นที่คุณใช้เพื่อรับโทเค็นเพื่อการเข้าถึงใหม่ได้ โทเค็นการรีเฟรชจะใช้ได้จนกว่าผู้ใช้จะเพิกถอนสิทธิ์เข้าถึง |
scope |
ค่าของช่องนี้จะตั้งค่าเป็น Openid สําหรับ Use Case การลงชื่อเข้าใช้บัญชีที่ลิงก์เสมอ |
token_type |
ประเภทของโทเค็นที่ส่งกลับ ในขณะนี้ ระบบจะตั้งค่าช่องนี้เป็น Bearer เสมอ |
ตัวอย่างการตอบกลับ
HTTP/1.1 200 OK
Content-type: application/json; charset=utf-8
{
"access_token": "Google-access-token",
"id_token": "Google-ID-token",
"expires_in": 3599,
"token_type": "Bearer",
"scope": "openid",
"refresh_token": "Google-refresh-token"
}
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
code=Google authorization code
&grant_type=authorization_code
&client_id=Google client id
&client_secret=Google client secret
ตรวจสอบการตอบสนองของโทเค็นรหัส
Validate and decode the JWT assertion
You can validate and decode the JWT assertion by using a JWT-decoding library for your language. Use Google's public keys, available in JWK or PEM formats, to verify the token's signature.
When decoded, the JWT assertion looks like the following example:
{ "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 "email_verified": true, // true, if Google has verified the email address "hd": "example.com", // If present, the host domain of the user's GSuite email address // If present, a URL to user's profile picture "picture": "https://lh3.googleusercontent.com/a-/AOh14GjlTnZKHAeb94A-FmEbwZv7uJD986VOF1mJGb2YYQ", "locale": "en_US" // User's locale, from browser or phone settings }
In addition to verifying the token's signature, verify that the assertion's
issuer (iss
field) is https://accounts.google.com
, that the audience
(aud
field) is your assigned client ID, and that the token has not expired
(exp
field).
Using the email
, email_verified
and hd
fields you can determine if
Google hosts and is authoritative for an email address. In cases where Google is
authoritative the user is currently known to be the legitimate account owner
and you may skip password or other challenges methods. Otherwise, these methods
can be used to verify the account prior to linking.
Cases where Google is authoritative:
email
has a@gmail.com
suffix, this is a Gmail account.email_verified
is true andhd
is set, this is a G Suite account.
Users may register for Google Accounts without using Gmail or G Suite. When
email
does not contain a @gmail.com
suffix and hd
is absent Google is not
authoritative and password or other challenge methods are recommended to verify
the user. email_verfied
can also be true as Google initially verified the
user when the Google account was created, however ownership of the third party
email account may have since changed.