ตั้งค่า JavaScript Consumer SDK
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
JavaScript Consumer SDK ช่วยให้แอปสำหรับผู้บริโภคแสดงตำแหน่งของ
ยานพาหนะและตำแหน่งอื่นๆ ที่น่าสนใจซึ่งติดตามใน Fleet Engine บนแผนที่บนเว็บ
ได้ ซึ่งจะช่วยให้ผู้ใช้ที่เป็นผู้บริโภคเห็นความคืบหน้าของการจัดส่ง
คู่มือนี้ถือว่าคุณได้ตั้งค่า Fleet Engine ด้วยโปรเจ็กต์ Google Cloud และคีย์ API ที่เชื่อมโยงแล้ว
ดูรายละเอียดได้ที่ Fleet Engine
คุณตั้งค่า JavaScript Consumer SDK โดยทำตามขั้นตอนต่อไปนี้
- เปิดใช้ Maps JavaScript API
- ตั้งค่าการให้สิทธิ์
เปิดใช้ Maps JavaScript API
เปิดใช้ Maps JavaScript API ในโปรเจ็กต์ Google Cloud Console ที่คุณใช้
สําหรับอินสแตนซ์ Fleet Engine ดูรายละเอียดเพิ่มเติมได้ที่เปิดใช้ API ในเอกสารประกอบของ Maps JavaScript API
ตั้งค่าการให้สิทธิ์
Fleet Engine กำหนดให้ใช้ JSON Web Token (JWT) สำหรับการเรียกเมธอด API
จากสภาพแวดล้อมที่มีความน่าเชื่อถือต่ำ ได้แก่ สมาร์ทโฟนและเบราว์เซอร์
JWT มาจากเซิร์ฟเวอร์ของคุณ มีการลงชื่อ เข้ารหัส และส่งไปยังไคลเอ็นต์
สำหรับการโต้ตอบกับเซิร์ฟเวอร์ในภายหลังจนกว่าจะหมดอายุหรือไม่ถูกต้องอีกต่อไป
รายละเอียดสำคัญ
แอปสำหรับผู้บริโภคควรตรวจสอบสิทธิ์ผู้ใช้ปลายทางด้วยบทบาท
delivery_consumer
จากโปรเจ็กต์ Google Cloud เพื่อแสดงเฉพาะข้อมูลสำหรับผู้บริโภค
ด้วยวิธีนี้ Fleet Engine จะกรองและปกปิดข้อมูลอื่นๆ ทั้งหมด
ในการตอบกลับ เช่น ในระหว่างงานที่ไม่มีความพร้อม
จะไม่มีการแชร์ข้อมูลตำแหน่งกับผู้ใช้ปลายทาง ดู
บทบาทของบัญชีบริการสำหรับงานที่กำหนดเวลาไว้
การให้สิทธิ์ทำงานอย่างไร
การให้สิทธิ์ด้วยข้อมูล Fleet Engine เกี่ยวข้องกับการติดตั้งใช้งานทั้งฝั่งเซิร์ฟเวอร์และฝั่งไคลเอ็นต์
การให้สิทธิ์ฝั่งเซิร์ฟเวอร์
ก่อนที่จะตั้งค่าการตรวจสอบสิทธิ์และการให้สิทธิ์ในแอปพลิเคชันบนเว็บ เซิร์ฟเวอร์แบ็กเอนด์ต้องออกโทเค็นเว็บ JSON ให้กับแอปพลิเคชันบนเว็บเพื่อเข้าถึง Fleet Engine ได้ แอปพลิเคชันบนเว็บของคุณ
จะส่ง JWT เหล่านี้ไปพร้อมกับคำขอเพื่อให้ Fleet Engine รู้จักคำขอว่า
ได้รับการตรวจสอบสิทธิ์และได้รับอนุญาตให้เข้าถึงข้อมูลใน
คำขอ ดูวิธีการติดตั้งใช้งาน JWT ฝั่งเซิร์ฟเวอร์ได้ที่ออกโทเค็นเว็บ JSON ในส่วนข้อควรทราบเกี่ยวกับ Fleet Engine
โดยเฉพาะอย่างยิ่ง โปรดคำนึงถึงสิ่งต่อไปนี้สำหรับ JavaScript Consumer SDK สำหรับการติดตามการจัดส่ง
การให้สิทธิ์ฝั่งไคลเอ็นต์
เมื่อใช้ JavaScript Consumer SDK ระบบจะขอโทเค็นจากเซิร์ฟเวอร์โดยใช้
ตัวดึงข้อมูลโทเค็นการให้สิทธิ์ โดยจะดำเนินการเมื่อมีสิ่งต่อไปนี้
มิฉะนั้น JavaScript Consumer SDK จะใช้โทเค็นที่ออกให้ก่อนหน้านี้ซึ่งยังใช้งานได้ และจะไม่เรียกใช้ Fetcher
สร้างตัวดึงข้อมูลโทเค็นการให้สิทธิ์
สร้างตัวดึงข้อมูลโทเค็นการให้สิทธิ์โดยใช้หลักเกณฑ์ต่อไปนี้
ตัวดึงข้อมูลต้องส่งคืนโครงสร้างข้อมูลที่มี 2 ฟิลด์ ซึ่งอยู่ใน
Promise
ดังนี้
โปรแกรมดึงข้อมูลควรเรียก URL ในเซิร์ฟเวอร์เพื่อดึงโทเค็น URL นี้
--SERVER_TOKEN_URL
--ขึ้นอยู่กับการติดตั้งใช้งานแบ็กเอนด์ URL ตัวอย่างต่อไปนี้ใช้สำหรับแบ็กเอนด์ของแอปตัวอย่างใน GitHub
https://SERVER_URL/token/delivery_consumer/TRACKING_ID
ตัวอย่าง - สร้างตัวดึงข้อมูลโทเค็นการตรวจสอบสิทธิ์
ตัวอย่างต่อไปนี้แสดงวิธีสร้างเครื่องมือดึงข้อมูลโทเค็นการให้สิทธิ์
JavaScript
async function authTokenFetcher(options) {
// options is a record containing two keys called
// serviceType and context. The developer should
// generate the correct SERVER_TOKEN_URL and request
// based on the values of these fields.
const response = await fetch(SERVER_TOKEN_URL);
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
return {
token: data.Token,
expiresInSeconds: data.ExpiresInSeconds
};
}
TypeScript
function authTokenFetcher(options: {
serviceType: google.maps.journeySharing.FleetEngineServiceType,
context: google.maps.journeySharing.AuthTokenContext,
}): Promise<google.maps.journeySharing.AuthToken> {
// The developer should generate the correct
// SERVER_TOKEN_URL based on options.
const response = await fetch(SERVER_TOKEN_URL);
if (!response.ok) {
throw new Error(response.statusText);
}
const data = await response.json();
return {
token: data.token,
expiresInSeconds: data.ExpiresInSeconds,
};
}
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-04 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-09-04 UTC"],[[["\u003cp\u003eThe JavaScript Consumer SDK enables your web application to display the real-time location of vehicles and other points of interest tracked within Fleet Engine, enhancing shipment visibility for consumers.\u003c/p\u003e\n"],["\u003cp\u003ePrior to implementation, ensure you have a Google Cloud project configured with Fleet Engine, including necessary API keys and the Maps JavaScript API enabled.\u003c/p\u003e\n"],["\u003cp\u003eSecure your application by setting up authorization using JSON Web Tokens (JWTs) issued by your backend server, enabling authenticated access to Fleet Engine data.\u003c/p\u003e\n"],["\u003cp\u003eDevelop an authorization token fetcher on the client-side to retrieve and manage JWTs, ensuring seamless communication between your web application and Fleet Engine.\u003c/p\u003e\n"],["\u003cp\u003eRemember to adhere to the provided guidelines for generating JWTs and implementing the authorization token fetcher to maintain security and data integrity.\u003c/p\u003e\n"]]],[],null,["With the JavaScript Consumer SDK, your consumer app can show the location of\nvehicles and other locations of interest tracked in Fleet Engine on a web-based\nmap. This allows your consumer users to see the progress of their shipments.\nThis guide assumes you have set up Fleet Engine with its associated\nGoogle Cloud project and API keys. See [Fleet Engine](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/create-project) for details.\n\nYou set up the JavaScript Consumer SDK following these steps:\n\n1. [Enable the Maps JavaScript API](#enable).\n2. [Set up authorization](#set-up-auth).\n\nEnable the Maps JavaScript API\n\nEnable the Maps JavaScript API in the Google Cloud Console project that you use\nfor your Fleet Engine instance. For more details, see [Enable APIs](/maps/documentation/javascript/cloud-setup#enabling-apis) in the\nMaps JavaScript API documentation.\n\nSet up authorization\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\nYour consumer app should authenticate your end users with the `delivery_consumer` role from your Google Cloud project to return only consumer-specific information. In this way, Fleet Engine filters and redacts all other information in the responses. For example, during an unavailability task, no location information is shared with an end user. See [Service account\nroles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts#scheduled-tasks) for scheduled tasks.\n\nHow does authorization work?\n\nAuthorization with Fleet Engine data involves both server-side and client-side\nimplementation.\n\nServer-side authorization\n\nBefore you set up authentication and authorization in your web-based\napplication, your backend server must be able to issue JSON Web Tokens to your\nweb-based application for access to Fleet Engine. Your web-based application\nsends these JWTs with its requests so Fleet Engine recognizes the requests as\nauthenticated and authorized to access the data in the\nrequest. For instructions on server-side JWT implementation, see [Issue JSON Web\nTokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt) under **Fleet Engine Essentials**.\nSpecifically, keep in mind the following for the JavaScript Consumer SDK for tracking shipments:\n\n\u003cbr /\u003e\n\n- [General guidelines](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt#general_guidelines) for issuing JSON Web Tokens\n- [Scheduled tasks JWT guidelines](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt#for_scheduled_tasks)\n- [Example token for a consumer app](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt#example_token_for_a_consumer_app)\n\nClient-side authorization\n\nWhen you use the JavaScript Consumer SDK, it requests a token from the server using an\nauthorization token fetcher. It does this when any of the following is true:\n\n- No valid token exists, such as when the SDK hasn't called the fetcher on a\n fresh page load, or when the fetcher hasn't returned with a token.\n\n- The token has expired.\n\n- The token is within one minute of expiring.\n\nOtherwise, the JavaScript Consumer SDK uses the previously-issued, valid token and does not\ncall the fetcher.\n\nCreate an authorization token fetcher\n\nCreate your authorization token fetcher using these guidelines:\n\n- **The fetcher must return a data structure with two fields** , wrapped in a\n `Promise` as follows:\n\n - A string `token`.\n\n - A number `expiresInSeconds`. A token expires in this amount of time\n after fetching. The authentication token fetcher must pass the expiry\n time in seconds, from the time of fetching to the library as shown in\n the example.\n\n- **The fetcher should call a URL on your server** to retrieve a token. This\n URL--the `SERVER_TOKEN_URL`--depends on your backend implementation. The\n following example URL is for the [sample app backend on GitHub](https://github.com/googlemaps/last-mile-fleet-solution-samples/tree/main/backend):\n\n - `https://SERVER_URL/token/delivery_consumer/TRACKING_ID`\n\nExample - Create an authentication token fetcher\n\nThe following examples show how to create an authorization token fetcher: \n\nJavaScript \n\n async function authTokenFetcher(options) {\n // options is a record containing two keys called\n // serviceType and context. The developer should\n // generate the correct SERVER_TOKEN_URL and request\n // based on the values of these fields.\n const response = await fetch(SERVER_TOKEN_URL);\n if (!response.ok) {\n throw new Error(response.statusText);\n }\n const data = await response.json();\n return {\n token: data.Token,\n expiresInSeconds: data.ExpiresInSeconds\n };\n }\n\nTypeScript \n\n function authTokenFetcher(options: {\n serviceType: google.maps.journeySharing.FleetEngineServiceType,\n context: google.maps.journeySharing.AuthTokenContext,\n }): Promise\u003cgoogle.maps.journeySharing.AuthToken\u003e {\n // The developer should generate the correct\n // SERVER_TOKEN_URL based on options.\n const response = await fetch(SERVER_TOKEN_URL);\n if (!response.ok) {\n throw new Error(response.statusText);\n }\n const data = await response.json();\n return {\n token: data.token,\n expiresInSeconds: data.ExpiresInSeconds,\n };\n }\n\nWhat's next\n\n- [Follow a shipment](/maps/documentation/mobility/journey-sharing/scheduled/shipment-tracking/follow)"]]