JSON Web Token
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
JSON Web Token (JWT) 是一種開放式網路標準,用於驗證及授權用戶端與伺服器之間的資訊交換。應用程式使用者首次使用適當角色憑證登入時,伺服器會建立並傳回經過編碼及數位簽章的 JWT,供後續要求使用。這個程序會驗證使用者身分,並根據帳戶角色授權使用者存取路徑、服務和資源。
從低信任度環境 (智慧型手機和瀏覽器) 呼叫 API 方法時,Fleet Engine 必須使用 JSON Web Token (JWT)。
JWT 會在伺服器上產生、簽署、加密,並傳遞至用戶端,供後續伺服器互動使用,直到 JWT 過期或失效為止。
重要詳細資料
與 API 金鑰不同,JWT 的效期較短,且僅限角色授權執行的作業。如要進一步瞭解 JWT,請參閱維基百科的「JSON Web Token」。如要進一步瞭解存取角色,請參閱本指南中的「服務帳戶角色」。
JWT 元素
JWT 包含標頭和聲明部分。標頭部分包含從服務帳戶取得的私密金鑰和加密演算法等資訊。聲明部分包含 JWT 的建立時間、存留時間、JWT 聲明可存取的服務,以及其他授權資訊 (例如運送車輛 ID),可限制存取範圍。
下表提供 JWT 欄位的一般說明,以及在 Fleet Engine Cloud 專案中尋找這些欄位值的具體資訊。
JWT 標頭欄位
欄位 |
說明 |
alg |
要使用的演算法。`RS256`。 |
typ |
權杖類型。`JWT`。 |
兒童 |
服務帳戶的私密金鑰 ID。您可以在服務帳戶 JSON 檔案的 private_key_id 欄位中找到這個值。請務必使用具備正確權限等級的服務帳戶金鑰。 |
JWT 憑證附加資訊欄位
欄位 |
說明 |
iss |
服務帳戶的電子郵件地址,位於服務帳戶 JSON 檔案的 client_email 欄位中。 |
替補球員 |
服務帳戶的電子郵件地址,位於服務帳戶 JSON 檔案的 client_email 欄位中。 |
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 起經過的秒數表示。如果時間戳記超過一小時,要求就會失敗。 |
授權 |
視用途而定,可能包含 deliveryvehicleid 、trackingid 、taskid 或 taskids 。
如要指定 taskid,授權範圍必須是陣列,且格式如下:
"taskids": ["task_id_one","task_id_two"]
或
"taskids": ["*"] |
Fleet Engine JWT 憑證附加資訊
Fleet Engine 會使用私有聲明。使用私有聲明可確保只有授權用戶端能存取自己的資料。
舉例來說,當伺服器為駕駛人的行動裝置核發 JSON 網頁權杖時,應包含 vehicleid
聲明或 deliveryvehicleid
聲明,並以該駕駛人的車輛 ID 做為值。然後,視駕駛員角色而定,JWT 只會啟用特定車輛 ID 的存取權,不會啟用任何其他任意車輛 ID。
Fleet Engine 使用下列私人聲明:
隨選行程
-
vehicleid
:
- 無論是行程或車輛作業,Driver SDK 一律會使用這項聲明。Fleet Engine 後端會先確認車輛與要求行程相關聯,再進行修改。
-
JWT 可涵蓋車輛和行程作業,即使並非必要,也能簡化 JWT 簽署實作程序。
-
tripid
:
- Consumer SDK 一律會使用這項聲明。
-
即使並非必要,JWT 仍可涵蓋車輛和行程作業,這有助於簡化權杖簽署實作程序。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\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."]]