توفّر حزمة تطوير البرامج (SDK) للمستهلك التفويض باستخدام رموز JSON المميّزة للويب. رمز JSON المميّز للويب (JWT) هو رمز تفويض يقدّم مطالبة واحدة أو أكثر بشأن خدمة معيّنة.
تستخدم حزمة تطوير البرامج (SDK) للمستهلك رمز JSON المميّز للويب الذي يوفّره التطبيق للتواصل مع Fleet Engine. للاطّلاع على تفاصيل الرموز المميّزة التي يتوقّعها خادم Fleet Engine، يُرجى مراجعة رموز JSON المميّزة للويب وإصدار رموز JSON المميّزة للويب.
يوفّر رمز التفويض إمكانية الوصول إلى خدمات Fleet Engine التالية:
TripService: يمنح حزمة تطوير البرامج (SDK) للمستهلك إذن الوصول إلى تفاصيل الرحلة، بما في ذلك موقع المركبة ومسارها والوقت المقدَّر للوصول. يجب أن تتضمّن الرموز المميّزة الخاصة بالتفويض لخدمة الرحلات مطالبةtripid:TRIP_IDفي عنوانauthorizationالخاص بالرمز المميّز، حيث يمثّلTRIP_IDرقم تعريف الرحلة عند الطلب التي تتم مشاركتها.VehicleService: يوفّر هذا الإذن لحزمة SDK للمستهلك معلومات عن الموقع الجغرافي التقريبي للمركبة لعرض طبقة كثافة المركبات وتقدير الوقت المتوقّع للوصول إلى نقطة الركوب. بما أنّ حزمة تطوير البرامج (SDK) للمستهلك تستخدم المواقع الجغرافية التقريبية فقط، لا تتطلّب رموز التفويض لخدمة المركبات مطالبةvehicleid.
ما هو الرمز المميّز؟
يتطلّب Fleet Engine استخدام رموز JSON المميّزة للويب (JWT) لطلبات إجراءات واجهة برمجة التطبيقات من البيئات المنخفضة الثقة: الهواتف الذكية والمتصفّحات.
ينشأ رمز JWT على الخادم الخاص بك، ويتم توقيعه وتشفيره وتمريره إلى العميل للتفاعلات اللاحقة مع الخادم إلى أن تنتهي صلاحيته أو يصبح غير صالح.
التفاصيل الأساسية
- استخدِم بيانات الاعتماد التلقائية للتطبيق للمصادقة على Fleet Engine ومنحه الإذن.
- استخدِم حساب خدمة مناسبًا لتوقيع رموز JWT. يمكنك الاطّلاع على أدوار حساب خدمة Fleet Engine في أساسيات Fleet Engine.
لمزيد من المعلومات حول رموز JSON المميّزة للويب، يُرجى الاطّلاع على رموز JSON المميّزة للويب في أساسيات Fleet Engine.
كيف يحصل العملاء على الرموز المميّزة؟
بعد أن يسجّل السائق أو المستهلك الدخول إلى تطبيقك باستخدام بيانات اعتماد التفويض المناسبة، يجب أن تستخدم أي تعديلات يتم إجراؤها من ذلك الجهاز رموز التفويض المميزة المناسبة، والتي تُبلغ Fleet Engine بالأذونات الخاصة بالتطبيق.
بصفتك مطوّرًا، يجب أن يوفّر تنفيذ العميل إمكانية إجراء ما يلي:
- احصل على رمز JSON مميّز للويب من خادمك.
- أعِد استخدام الرمز المميّز إلى أن تنتهي صلاحيته لتقليل عمليات إعادة تحميل الرمز المميّز.
- يجب إعادة تحميل الرمز المميّز عند انتهاء صلاحيته.
تنشئ الفئة AuthTokenFactory رموز التفويض المميّزة عند إشعار بشأن الموقع الجغرافي. يجب أن تجمّع حزمة SDK الرموز المميّزة مع معلومات التعديل
لإرسالها إلى Fleet Engine. تأكَّد من أنّ عملية التنفيذ من جهة الخادم يمكنها إصدار الرموز المميّزة قبل إعداد حزمة تطوير البرامج (SDK).
لمعرفة تفاصيل الرموز المميّزة التي تتوقّعها خدمة Fleet Engine، يُرجى الاطّلاع على إصدار رموز JSON المميّزة للويب لخدمة Fleet Engine.
مثال على أداة جلب رمز التفويض المميّز
يوضّح مثال الرمز التالي كيفية تنفيذ دالة استدعاء لرمز التفويض.
جافا
class JsonAuthTokenFactory implements AuthTokenFactory {
private static final String TOKEN_URL =
"https://yourauthserver.example/token";
private static class CachedToken {
String tokenValue;
long expiryTimeMs;
String tripId;
}
private CachedToken token;
/*
* This method is called on a background thread. Blocking is OK. However, be
* aware that no information can be obtained from Fleet Engine until this
* method returns.
*/
@Override
public String getToken(AuthTokenContext context) {
// If there is no existing token or token has expired, go get a new one.
String tripId = context.getTripId();
if (tripId == null) {
throw new RuntimeException("Trip ID is missing from AuthTokenContext");
}
if (token == null || System.currentTimeMillis() > token.expiryTimeMs ||
!tripId.equals(token.tripId)) {
token = fetchNewToken(tripId);
}
return token.tokenValue;
}
private static CachedToken fetchNewToken(String tripId) {
String url = TOKEN_URL + "/" + tripId;
CachedToken token = new CachedToken();
try (Reader r = new InputStreamReader(new URL(url).openStream())) {
com.google.gson.JsonObject obj
= com.google.gson.JsonParser.parseReader(r).getAsJsonObject();
token.tokenValue = obj.get("ServiceToken").getAsString();
token.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 5 minutes from that time.
*/
token.expiryTimeMs -= 5 * 60 * 1000;
} catch (IOException e) {
/*
* It's OK to throw exceptions here. The error listeners will receive the
* error thrown here.
*/
throw new RuntimeException("Could not get auth token", e);
}
token.tripId = tripId;
return token;
}
}
Kotlin
class JsonAuthTokenFactory : AuthTokenFactory() {
private var token: CachedToken? = null
/*
* This method is called on a background thread. Blocking is OK. However, be
* aware that no information can be obtained from Fleet Engine until this
* method returns.
*/
override fun getToken(context: AuthTokenContext): String {
// If there is no existing token or token has expired, go get a new one.
val tripId =
context.getTripId() ?:
throw RuntimeException("Trip ID is missing from AuthTokenContext")
if (token == null || System.currentTimeMillis() > token.expiryTimeMs ||
tripId != token.tripId) {
token = fetchNewToken(tripId)
}
return token.tokenValue
}
class CachedToken(
var tokenValue: String? = "",
var expiryTimeMs: Long = 0,
var tripId: String? = "",
)
private companion object {
const val TOKEN_URL = "https://yourauthserver.example/token"
fun fetchNewToken(tripId: String) {
val url = "$TOKEN_URL/$tripId"
val token = CachedToken()
try {
val reader = InputStreamReader(URL(url).openStream())
reader.use {
val obj = com.google.gson.JsonParser.parseReader(r).getAsJsonObject()
token.tokenValue = obj.get("ServiceToken").getAsString()
token.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 5 minutes from that time.
*/
token.expiryTimeMs -= 5 * 60 * 1000
}
} catch (e: IOException) {
/*
* It's OK to throw exceptions here. The error listeners will receive the
* error thrown here.
*/
throw RuntimeException("Could not get auth token", e)
}
token.tripId = tripId
return token
}
}
}
الخطوات التالية
إعداد حزمة تطوير البرامج (SDK) للمستهلك