โทเค็นเว็บ JSON
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
โทเค็นเว็บ JSON (JWT) เป็นมาตรฐานเว็บแบบเปิดที่ใช้สำหรับการตรวจสอบสิทธิ์
และให้สิทธิ์การแลกเปลี่ยนข้อมูลระหว่างไคลเอ็นต์กับเซิร์ฟเวอร์ เมื่อผู้ใช้แอป
ลงชื่อเข้าใช้ครั้งแรกด้วยข้อมูลเข้าสู่ระบบที่มีบทบาทที่เหมาะสม เซิร์ฟเวอร์จะสร้าง
และส่งคืน JWT ที่เข้ารหัสและลงชื่อแบบดิจิทัลเพื่อใช้กับคำขอที่ตามมา
กระบวนการนี้จะตรวจสอบสิทธิ์ผู้ใช้และให้สิทธิ์ผู้ใช้เข้าถึงเส้นทาง
บริการ และทรัพยากรตามบทบาทของบัญชี
Fleet Engine กำหนดให้ใช้ JSON Web Token (JWT) สำหรับการเรียกเมธอด API
จากสภาพแวดล้อมที่มีความน่าเชื่อถือต่ำ ได้แก่ สมาร์ทโฟนและเบราว์เซอร์
JWT มาจากเซิร์ฟเวอร์ของคุณ มีการลงชื่อ เข้ารหัส และส่งไปยังไคลเอ็นต์
สำหรับการโต้ตอบกับเซิร์ฟเวอร์ในภายหลังจนกว่าจะหมดอายุหรือไม่ถูกต้องอีกต่อไป
รายละเอียดสำคัญ
JWT มีอายุสั้นและจำกัดการดำเนินการเฉพาะการดำเนินการที่บทบาทได้รับอนุญาตให้ดำเนินการเท่านั้น ซึ่งแตกต่างจากคีย์ API ดูข้อมูลเพิ่มเติมเกี่ยวกับ JWT ได้ที่
โทเค็นเว็บ JSON ใน Wikipedia ดูรายละเอียดเกี่ยวกับบทบาทการเข้าถึงได้ที่บทบาทของบัญชีบริการในคู่มือนี้
องค์ประกอบ JWT
JWT มีส่วนหัวและส่วนการอ้างสิทธิ์ ส่วนหัวมีข้อมูล เช่น คีย์ส่วนตัวที่ได้จากบัญชีบริการ และอัลกอริทึมการเข้ารหัส ส่วนการอ้างสิทธิ์มีข้อมูล เช่น เวลาสร้างของ JWT
อายุการใช้งาน บริการที่ JWT อ้างสิทธิ์
เข้าถึง และข้อมูลการให้สิทธิ์อื่นๆ เพื่อกำหนดขอบเขตการเข้าถึง เช่น
รหัสยานพาหนะนำส่ง
ตารางต่อไปนี้แสดงรายละเอียดคำอธิบายเกี่ยวกับฟิลด์ JWT โดยทั่วไป
รวมถึงข้อมูลเฉพาะเกี่ยวกับตำแหน่งที่คุณดูค่าของฟิลด์เหล่านี้
ได้ในโปรเจ็กต์ Cloud ของ Fleet Engine
ฟิลด์ส่วนหัวของ JWT
ฟิลด์ |
คำอธิบาย |
alg |
อัลกอริทึมที่จะใช้ `RS256` |
typ |
ประเภทของโทเค็น `JWT` |
บุตรหลาน |
รหัสคีย์ส่วนตัวของบัญชีบริการ คุณดูค่านี้ได้ในฟิลด์
private_key_id ของไฟล์ JSON ของบัญชีบริการ ตรวจสอบ
ว่าใช้คีย์จากบัญชีบริการที่มีสิทธิ์ในระดับที่ถูกต้อง |
ฟิลด์การอ้างสิทธิ์ JWT
ฟิลด์ |
คำอธิบาย |
iss |
อีเมลของบัญชีบริการ ซึ่งอยู่ในฟิลด์ client_email ของไฟล์ JSON ของบัญชีบริการ |
สำรอง |
อีเมลของบัญชีบริการ ซึ่งอยู่ในฟิลด์ client_email ของไฟล์ JSON ของบัญชีบริการ |
aud |
SERVICE_NAME ของบัญชีบริการ
ในกรณีนี้คือ https://fleetengine.googleapis.com/
|
iat |
การประทับเวลาเมื่อสร้าง JWT โดยระบุเป็นวินาที
ที่ผ่านไปตั้งแต่ 00:00:00 UTC, January 1, 1970 เผื่อเวลา 10 นาทีสำหรับความเอียง
หากการประทับเวลาอยู่ในอดีตหรืออนาคตนานเกินไป เซิร์ฟเวอร์อาจรายงานข้อผิดพลาด |
exp |
การประทับเวลาเมื่อ JWT หมดอายุ โดยระบุเป็นวินาทีที่ผ่านไป
นับตั้งแต่ 00:00:00 UTC, January 1, 1970 คำขอจะล้มเหลวหาก
การประทับเวลาอยู่เกิน 1 ชั่วโมงในอนาคต |
การให้สิทธิ์ |
อาจมี deliveryvehicleid ,
trackingid , taskid หรือ taskids ขึ้นอยู่กับกรณีการใช้งาน
หากระบุ taskids ขอบเขตการให้สิทธิ์ต้องเป็นอาร์เรย์ในรูปแบบใดรูปแบบหนึ่งต่อไปนี้
"taskids": ["task_id_one","task_id_two"]
หรือ
"taskids": ["*"] |
การอ้างสิทธิ์ JWT ของ Fleet Engine
Fleet Engine ใช้การอ้างสิทธิ์ส่วนตัว การใช้การอ้างสิทธิ์ส่วนตัวช่วยให้มั่นใจได้ว่ามีเพียงไคลเอ็นต์ที่ได้รับอนุญาตเท่านั้นที่เข้าถึงข้อมูลของตนเองได้
ตัวอย่างเช่น เมื่อเซิร์ฟเวอร์ออกโทเค็นเว็บ JSON สำหรับอุปกรณ์เคลื่อนที่ของคนขับ
โทเค็นควรมีvehicleid
อ้างสิทธิ์หรือdeliveryvehicleid
อ้างสิทธิ์ที่มีค่ารหัสยานพาหนะของคนขับรายนั้น จากนั้น
JWT จะเปิดใช้สิทธิ์เข้าถึงเฉพาะรหัสยานพาหนะที่เฉพาะเจาะจงเท่านั้น และไม่ใช่รหัสยานพาหนะอื่นๆ โดยพลการ ทั้งนี้ขึ้นอยู่กับบทบาทของคนขับ
Fleet Engine ใช้การอ้างสิทธิ์ส่วนตัวต่อไปนี้
การเดินทางแบบออนดีมานด์
-
vehicleid
:
- Driver SDK จะใช้การอ้างสิทธิ์นี้เสมอ ไม่ว่าจะดำเนินการในการเดินทาง
หรือยานพาหนะ แบ็กเอนด์ของ Fleet Engine จะตรวจสอบว่ายานพาหนะเชื่อมโยงกับการเดินทางที่ขอแล้วก่อนที่จะทำการแก้ไข
-
JWT สามารถครอบคลุมทั้งการดำเนินการเกี่ยวกับยานพาหนะและการเดินทาง แม้ว่าจะไม่จำเป็นก็ตาม ซึ่งอาจช่วยลดความซับซ้อนในการติดตั้งใช้งานการลงนาม JWT
-
tripid
:
- Consumer SDK จะใช้การอ้างสิทธิ์นี้เสมอ
-
JWT สามารถครอบคลุมทั้งการดำเนินการเกี่ยวกับยานพาหนะและการเดินทาง
แม้ว่าจะไม่จำเป็นก็ตาม ซึ่งอาจช่วยลดความซับซ้อนในการติดตั้งใช้งานการลงนามโทเค็น
งานที่กำหนดเวลาไว้
-
deliveryvehicleid
ใช้เมื่อเรียก API ต่อรถนำส่ง
-
taskid
ใช้เมื่อเรียก API ต่องาน
-
taskids
ใช้เมื่อโทร
BatchCreateTasksAPI
การอ้างสิทธิ์นี้ต้องอยู่ในรูปแบบอาร์เรย์
และอาร์เรย์ควรมีรหัสงานทั้งหมดที่จำเป็นต่อการดำเนินการตามคำขอ
ให้เสร็จสมบูรณ์ อย่าใส่การกล่าวอ้าง delivervehicleid
,
trackingid
หรือ taskid
-
trackingid
ใช้เมื่อโทรหา
GetTaskTrackingInfoAPI
การอ้างสิทธิ์ต้องตรงกับรหัสติดตามในคำขอ อย่าใส่การกล่าวอ้าง delivervehicleid
,
taskid
หรือ taskids
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eJSON Web Tokens (JWTs) are required for API calls to Fleet Engine from low-trust environments like smartphones and browsers, providing authentication and authorization.\u003c/p\u003e\n"],["\u003cp\u003eJWTs are digitally signed by a service account on your server, a trusted environment, and passed to the client for secure communication with Fleet Engine.\u003c/p\u003e\n"],["\u003cp\u003eJWTs contain header and claim sections with information about the private key, encryption algorithm, token lifespan, and authorized access scopes like vehicle or trip IDs.\u003c/p\u003e\n"],["\u003cp\u003eFleet Engine utilizes private claims within JWTs to ensure data security and limit access to specific resources based on assigned roles and vehicle or task IDs.\u003c/p\u003e\n"],["\u003cp\u003eUnlike API keys, JWTs are short-lived and restrict operations to those permitted by the associated service account role, enhancing security.\u003c/p\u003e\n"]]],[],null,["# JSON Web Tokens\n\nA JSON Web Token (JWT) is an open web standard that's used for authenticating\nand authorizing information exchanges between a client and a server. When an app\nuser first signs in with the appropriate role credentials, the server creates\nand returns an encoded, digitally-signed JWT for use with subsequent requests.\nThis process both authenticates the user and authorizes them to access routes,\nservices, and resources based on their account role.\n\nFleet Engine requires the use of **JSON Web Tokens** (JWTs) for API method calls\nfrom **low-trust environments**: smartphones and browsers.\n\nA JWT originates on your server, is signed, encrypted, and passed to the client\nfor subsequent server interactions until it expires or is no longer valid.\n\n**Key details**\n\n- Use [Application Default Credentials](https://google.aip.dev/auth/4110) to authenticate and authorize against Fleet Engine.\n- Use an appropriate service account to sign JWTs. See [Fleet Engine serviceaccount](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts#fleet_engine_service_account_roles) roles in **Fleet Engine Basics**.\n\nUnlike API keys, JWTs are short lived and limit operations to only those that\nthe role is authorized to perform. For more information on JWTs, see\n[JSON Web Tokens](https://en.wikipedia.org/wiki/JSON_Web_Token) on Wikipedia. For detail on access roles, see\n[Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts) in this guide.\n\nJWT elements\n------------\n\nJWTs contain a header and a claim section. The header section contains\ninformation such as the private key obtained from service accounts, and the\nencryption algorithm. The claim section contains information such as the JWT's\ncreate time, time to live, the services that the JWT claims\naccess to, and other authorization information to scope access; for\nexample, the delivery vehicle ID.\n\nThe following table provides descriptive details about JWT fields in general,\nas well as specific information about where you can find the values for these\nfields in your Fleet Engine Cloud project.\n\n| **Field** | **Description** |\n|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| alg | The algorithm to use. \\`RS256\\`. |\n| typ | The type of token. \\`JWT\\`. |\n| kid | Your service account's private key ID. You can find this value in the `private_key_id` field of your service account JSON file. Make sure to use a key from a service account with the correct level of permissions. |\n[JWT header fields]\n\n| **Field** | **Description** |\n|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| iss | Your service account's email address, found in the `client_email` field of your service account JSON file. |\n| sub | Your service account's email address, found in the `client_email` field of your service account JSON file. |\n| aud | Your service account's `SERVICE_NAME`, in this case `https://fleetengine.googleapis.com/` |\n| iat | The timestamp when the JWT was created, specified in seconds elapsed since 00:00:00 `UTC, January 1, 1970`. Allow 10 minutes for skew. If the timestamp is too far in the past, or in the future, the server might report an error. |\n| exp | The timestamp when the JWT expires, specified in seconds elapsed since `00:00:00 UTC, January 1, 1970`. The request fails if the timestamp is more than one hour in the future. |\n| authorization | Depending on the use case, may contain `deliveryvehicleid`, `trackingid`, `taskid`, or `taskids`. If specifying taskids, the authorization scope must be an array in one of the following forms: `\"taskids\": [\"task_id_one\",\"task_id_two\"]` or `\"taskids\": [\"*\"]` |\n[JWT claims fields]\n\n### Fleet Engine JWT claims\n\nFleet Engine uses private claims. Using private claims ensures that only\nauthorized clients can access their own data.\n\nFor example, when your server issues a JSON Web Token for a driver's mobile\ndevice, it should contain either the `vehicleid` claim or the\n`deliveryvehicleid` claim with the value of that driver's vehicle ID. Then,\ndepending on the driver role, JWTs enable access only for the specific vehicle\nID and not any other arbitrary vehicle ID.\n\nFleet Engine uses the following private claims: \n\n### On-demand trips\n\n- **`vehicleid`** :\n - The Driver SDK always uses this claim, whether operating on a trip or vehicle. The Fleet Engine backend assures that the vehicle is associated with the requested trip before doing the modification.\n - The JWT can cover both vehicle and trip operations, even if not required, which may simplify the JWT signing implementation.\n- **`tripid`** :\n - The Consumer SDK always uses this claim.\n - The JWT can cover both vehicle and trip operations, even if not required, which may simplify the token signing implementation.\n\n### Scheduled tasks\n\n- `deliveryvehicleid`\n\n Use when calling per-delivery-vehicle\n APIs.\n- `taskid`\n\n Use when calling per-task APIs.\n- `taskids`\n\n Use when calling\n `BatchCreateTasksAPI`. This claim must be in array form,\n and the array should contain all task IDs necessary to complete the\n request. Don't include `delivervehicleid`,\n `trackingid`, or `taskid` claims.\n- `trackingid`\n\n Use when calling the\n `GetTaskTrackingInfoAPI`. The claim must match the tracking\n ID in the request. Don't include `delivervehicleid`,\n `taskid`, or `taskids` claims.\n\nWhat's next\n-----------\n\n- Read about [Fleet Engine security design](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/security-design) to understand the complete authentication flow.\n- Learn how to [Issue JSON Web Tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt) from your server."]]