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/MapServer
Whenever 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.76837809...
It 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
Solved! Go to Solution.
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.
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 @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!