Hi There,
We are using Kotlin Maps SDK (version : 200.8.0) in our Android App to display maps.
Our QA backend release new URL to display traffic heat layer.URL : https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServerWhenever app tries to load traffic layer using this url the SDK throws following error.An ArcGIS authentication error occurred when accessing https://www.arcgis.com. An API key or credential may be required. For more information see https://developers.arcgis.com/kotlin/security-and-authentication/.Following is my code to render traffic layer in Kotlin.
fun getTrafficLayer(accessToken: String): BasemapResult.AsLayer { ArcGISEnvironment.apiKey = ApiKey.create(accessToken)val newLayer = ArcGISMapImageLayer("https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer/") newLayer.name = "Traffic Layer" return BasemapResult.AsLayer(newLayer)}
I am getting accessToken from backend.although other layers (vector,satellite) able to load successfully with same API Key.I tried to load this traffic URL on web with access token and few other parameter and worked successfully.e.g. : https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer/export?bbox=2969828.768378092%2C-3115102.2103630807%2C2984695.7703795386%2C-3098763.7955671465&bboxSR=102100&imageSR=102100&size=778%2C855&dpi=96&format=png32&transparent=true&layers=show%3A4%2C51%2C7%2C12%2C54%2C15%2C20%2C57%2C23%2C28%2C60%2C31%2C48%2C63%2C43&f=image&token=ABC123It means there is no issue with access token.I dont know why it is failing to authenticate on Mobile only. Is this an API Key issue ? Does traffic service needs to be enabled for this particular API Key / Account ?Please help me resolved this issue.Regards,Prashant
It turns out this is a known limitation where API keys cannot be used to access ArcGIS map image layers directly. However, we can still get this to work by creating a PregeneratedTokenCredential with your API key and adding this to the ArcGIS credential store.Here's how that would look:
val trafficLayer = "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer/" val tokenInfo = TokenInfo( accessToken = apiKey.toString(), expirationDate = Instant.now().plus(Duration.ofHours(2)), //an expiration equal to or less than your API key expiration isSslRequired = true ) val trafficCredential = PregeneratedTokenCredential( url = trafficLayer, tokenInfo = tokenInfo ) // Ensure that you add the credential to the store prior to loading the layer ArcGISEnvironment.authenticationManager.ArcGISCredentialStore.add(trafficCredential) // Continue with loading the traffic layer
Hi Prashant, sorry about the delay in responding here. Can you please confirm a couple of details to help us with our investigation?
Hi @MikeWilburn , thank you for replying.following are the answer's to your questions respectively.1. We are using ArcGIS Online account to create access token.2. For both platforms (App and Web) we are using different access token. We also tried using Web access token in app but faced same error.
Hi @MikeWilburn , Thank you for the code. I will try this and let you know if this works.
Hi @MikeWilburn , your solution worked. Thank you very much !
Fantastic - glad to hear it!
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.