[英]How to detect expired authentication token with Mapbox SDK?
I'm using Android Mapbox SDK 7.0.0 and I give the SDK an authentication token with the given method: Mapbox.getInstance(applicationContext, mapboxToken)
我正在使用Android Mapbox SDK 7.0.0,并使用给定的方法为SDK提供了身份验证令牌:
Mapbox.getInstance(applicationContext, mapboxToken)
My authentication token, mapboxToken
, is working fine and the map is displayed. 我的身份验证令牌
mapboxToken
运行正常,并显示了地图。
But, how to detect when the authentication token expires? 但是,如何检测身份验证令牌何时过期?
I want to refresh this authentication token from time to time, but this also means that the Android app has to detect expired tokens in order to ask for a new one. 我想不定期刷新此身份验证令牌,但这也意味着Android应用程序必须检测过期的令牌才能请求新的令牌。
If no tile is in cache, then the MapboxView
triggers the OnDidFailLoadingMapListener
but if there are some tiles in cache, the map is loaded correctly, new tiles are not loaded and no callback is triggered. 如果缓存中没有图块,则
MapboxView
会触发OnDidFailLoadingMapListener
但是如果缓存中有一些图块,则将正确加载该图,不会加载新图块,也不会触发回调。
I found a solution to my problem. 我找到了解决问题的方法。 It's possible to override the Http client of the Mapbox SDK.
可以覆盖Mapbox SDK的Http客户端。 This workaround works for the Mapbox SDK provided for Android.
该解决方法适用于Android提供的Mapbox SDK。 Adding an interceptor we can interprete the Http code before returning the response to the Mapbox native library.
添加拦截器,我们可以在将响应返回给Mapbox本机库之前解释Http代码。
Here is my workaround to catch unauthorized HTTP codes: 这是我捕获未经授权的HTTP代码的解决方法:
val client = OkHttpClient.Builder().addInterceptor { chain ->
val response = chain.proceed(chain.request())
if (response.code() == 401) {
// Code that handles unauthorized Mapbox token
}
response
}.build()
HttpRequestImpl.setOkHttpClient(client)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.