รับโทเค็นการให้สิทธิ์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
โทเค็นคืออะไร
Fleet Engine กำหนดให้ใช้ JSON Web Token (JWT) สำหรับการเรียกเมธอด API
จากสภาพแวดล้อมที่มีความน่าเชื่อถือต่ำ ได้แก่ สมาร์ทโฟนและเบราว์เซอร์
JWT มาจากเซิร์ฟเวอร์ของคุณ มีการลงชื่อ เข้ารหัส และส่งไปยังไคลเอ็นต์
สำหรับการโต้ตอบกับเซิร์ฟเวอร์ในภายหลังจนกว่าจะหมดอายุหรือไม่ถูกต้องอีกต่อไป
รายละเอียดสำคัญ
ดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็นเว็บ JSON ได้ที่โทเค็นเว็บ JSON ในข้อควรทราบเกี่ยวกับ Fleet Engine
ไคลเอ็นต์จะรับโทเค็นได้อย่างไร
เมื่อคนขับหรือผู้บริโภคเข้าสู่ระบบแอปโดยใช้
ข้อมูลเข้าสู่ระบบการให้สิทธิ์ที่เหมาะสม การอัปเดตใดๆ ที่ออกให้อุปกรณ์นั้นจะต้องใช้
โทเค็นการให้สิทธิ์ที่เหมาะสม ซึ่งจะสื่อสารกับ Fleet Engine เกี่ยวกับ
สิทธิ์ของแอป
ในฐานะนักพัฒนาแอป การติดตั้งใช้งานฝั่งไคลเอ็นต์ควรมีความสามารถในการทำสิ่งต่อไปนี้
- ดึงข้อมูลโทเค็นเว็บ JSON จากเซิร์ฟเวอร์
- นำโทเค็นกลับมาใช้ใหม่จนกว่าจะหมดอายุเพื่อลดการรีเฟรชโทเค็น
- รีเฟรชโทเค็นเมื่อหมดอายุ
คลาส AuthTokenFactory
จะสร้างโทเค็นการให้สิทธิ์เมื่อมีการอัปเดตตำแหน่ง
SDK ต้องจัดแพ็กเกจโทเค็นพร้อมข้อมูลอัปเดตเพื่อส่งไปยัง Fleet Engine ตรวจสอบว่าการติดตั้งใช้งานฝั่งเซิร์ฟเวอร์
สามารถออกโทเค็นได้ก่อนที่จะเริ่มต้น SDK
ดูรายละเอียดของโทเค็นที่บริการ Fleet Engine คาดหวังได้ที่ออกโทเค็นเว็บ JSON สำหรับ Fleet Engine
ตัวอย่างเครื่องมือดึงข้อมูลโทเค็นการให้สิทธิ์
นี่คือโครงร่างการใช้งานของ AuthTokenFactory
class JsonAuthTokenFactory implements AuthTokenFactory {
private String vehicleServiceToken; // initially null
private long expiryTimeMs = 0;
private String vehicleId;
// This method is called on a thread whose only responsibility is to send
// location updates. Blocking is OK, but just know that no location updates
// can occur until this method returns.
@Override
public String getToken(AuthTokenContext authTokenContext) {
String vehicleId = requireNonNull(context.getVehicleId());
if (System.currentTimeMillis() > expiryTimeMs || !vehicleId.equals(this.vehicleId)) {
// The token has expired, go get a new one.
fetchNewToken(vehicleId);
}
return vehicleServiceToken;
}
private void fetchNewToken(String vehicleId) {
String url = "https://yourauthserver.example/token/" + vehicleId;
try (Reader r = new InputStreamReader(new URL(url).openStream())) {
com.google.gson.JsonObject obj
= com.google.gson.JsonParser.parseReader(r).getAsJsonObject();
vehicleServiceToken = obj.get("VehicleServiceToken").getAsString();
expiryTimeMs = obj.get("TokenExpiryMs").getAsLong();
// The expiry time could be an hour from now, but just to try and avoid
// passing expired tokens, we subtract 10 minutes from that time.
expiryTimeMs -= 10 * 60 * 1000;
this.vehicleId = vehicleId;
} catch (IOException e) {
// It's OK to throw exceptions here. The StatusListener you passed to
// create the DriverContext class will be notified and passed along the failed
// update warning.
throw new RuntimeException("Could not get auth token", e);
}
}
}
การติดตั้งใช้งานนี้ใช้ไคลเอ็นต์ HTTP ของ Java ในตัวเพื่อดึงโทเค็นในรูปแบบ JSON จากเซิร์ฟเวอร์การให้สิทธิ์ ไคลเอ็นต์จะบันทึกโทเค็น
เพื่อนำไปใช้ซ้ำ และดึงข้อมูลโทเค็นอีกครั้งหากโทเค็นเก่ามีเวลา
หมดอายุเหลือไม่เกิน 10 นาที
การติดตั้งใช้งานของคุณอาจทำงานแตกต่างออกไป เช่น การใช้เธรดเบื้องหลัง
เพื่อรีเฟรชโทเค็น
ดูไลบรารีของไคลเอ็นต์ที่พร้อมใช้งานสำหรับ Fleet Engine ได้ที่
ไลบรารีของไคลเอ็นต์สำหรับบริการงานที่กำหนดเวลาไว้
ขั้นตอนถัดไป
เริ่มต้น Driver SDK
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-05 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-05 UTC"],[[["\u003cp\u003eFleet Engine requires JSON Web Tokens (JWTs) for API calls from low-trust environments like smartphones and browsers, which are generated on your server and passed to the client.\u003c/p\u003e\n"],["\u003cp\u003eYour server should authenticate with Fleet Engine using Application Default Credentials and issue JWTs signed by an appropriate service account.\u003c/p\u003e\n"],["\u003cp\u003eClients must fetch, reuse, and refresh these JWTs to authorize their interactions with Fleet Engine, ensuring they have the necessary permissions.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eAuthTokenFactory\u003c/code\u003e class facilitates the process of generating and managing authorization tokens within your client application.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the provided links for detailed information about JWTs, service accounts, and client libraries.\u003c/p\u003e\n"]]],[],null,["# Get authorization tokens\n\nWhat is a token?\n----------------\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\nFor more information about JSON Web Tokens, see [JSON Web Tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/jwt) in\n**Fleet Engine Essentials**.\n\nHow do clients get tokens?\n--------------------------\n\nOnce a driver or consumer logs in to your app using the appropriate\nauthorization credentials, any updates issued from that device must use\nappropriate authorization tokens, which communicates to Fleet Engine the\npermissions for the app.\n\nAs the developer, your client implementation should provide the ability to do\nthe following:\n\n- Fetch a JSON Web Token from your server.\n- Reuse the token until it expires to minimize token refreshes.\n- Refresh the token when it expires.\n\nThe `AuthTokenFactory` class generates authorization tokens at location update\ntime. The SDK must package the tokens with the update\ninformation to send to Fleet Engine. Make sure that your server-side\nimplementation can issue tokens before initializing the SDK.\n\nFor details of the tokens expected by the Fleet Engine service, see [Issue JSON\nWeb Tokens](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/issue-jwt) for Fleet Engine.\n\nExample of an authorization token fetcher\n-----------------------------------------\n\nHere is a skeleton implementation of an `AuthTokenFactory`: \n\n class JsonAuthTokenFactory implements AuthTokenFactory {\n private String vehicleServiceToken; // initially null\n private long expiryTimeMs = 0;\n private String vehicleId;\n\n // This method is called on a thread whose only responsibility is to send\n // location updates. Blocking is OK, but just know that no location updates\n // can occur until this method returns.\n @Override\n public String getToken(AuthTokenContext authTokenContext) {\n String vehicleId = requireNonNull(context.getVehicleId());\n\n if (System.currentTimeMillis() \u003e expiryTimeMs || !vehicleId.equals(this.vehicleId)) {\n // The token has expired, go get a new one.\n fetchNewToken(vehicleId);\n }\n\n return vehicleServiceToken;\n }\n\n private void fetchNewToken(String vehicleId) {\n String url = \"https://yourauthserver.example/token/\" + vehicleId;\n\n try (Reader r = new InputStreamReader(new URL(url).openStream())) {\n com.google.gson.JsonObject obj\n = com.google.gson.JsonParser.parseReader(r).getAsJsonObject();\n vehicleServiceToken = obj.get(\"VehicleServiceToken\").getAsString();\n expiryTimeMs = obj.get(\"TokenExpiryMs\").getAsLong();\n\n // The expiry time could be an hour from now, but just to try and avoid\n // passing expired tokens, we subtract 10 minutes from that time.\n expiryTimeMs -= 10 * 60 * 1000;\n this.vehicleId = vehicleId;\n } catch (IOException e) {\n // It's OK to throw exceptions here. The StatusListener you passed to\n // create the DriverContext class will be notified and passed along the failed\n // update warning.\n throw new RuntimeException(\"Could not get auth token\", e);\n }\n }\n }\n\nThis particular implementation uses the built-in Java HTTP client to fetch a\ntoken in JSON format from the authorization server. The client saves the token\nfor reuse and re-fetches the token if the old token is within 10 minutes of its\nexpiry time.\n\nYour implementation may do things differently, such as using a background thread\nto refresh tokens.\n\nFor the available client libraries for Fleet Engine, see\n[Client libraries for scheduled tasks services](/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks).\n\nWhat's next\n-----------\n\n[Initialize the Driver SDK](/maps/documentation/mobility/driver-sdk/scheduled/android/initialize-sdk)"]]