简体   繁体   English

如何使用Mapbox SDK检测过期的身份验证令牌?

[英]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.

相关问题 如何使用 Mapbox Android SDK v10-beta20 向图块提供者提供身份验证令牌? - How to provide an authentication token to a tile provider with Mapbox Android SDK v10-beta20? 如何检测令牌是否已过期或未在应用服务器上注册firebase FCM通知? - How to detect if token is expired or not registred firebase FCM notification on app server? Firebase Android 身份验证失败:expired_token(身份验证令牌已过期) - Firebase Android Authentication failed: expired_token (Auth token is expired) 如何在Mapbox Sdk中过滤标记 - How to filter markers in Mapbox Sdk 如何处理匿名用户的过期令牌 - How to handle anonymous user's expired token 如何在令牌过期时注销 Android LiveData - How to Logout when Token is expired Android LiveData 如何在Android中检查访问令牌是否过期 - How to check if access token is expired or not in android 如何在Mapbox SDK中的LineString上设置文本? - How to set text on LineString in Mapbox SDK? 如何在Mapbox Android SDK中重定位MyLocationView? - How to relocate MyLocationView in Mapbox Android SDK? 如何使用Android SDK更改语言Mapbox - How to change language mapbox using android sdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM