Select to view content in your preferred language

Error when using PregeneratedTokenCredential to authenticate services.

223
3
Jump to solution
a month ago
locho
by
New Contributor

Screenshot 2025-01-13 at 09.15.27.png

It works unstable, sometimes it authenticates successfully, sometimes it fails.

Log error:

Unhandled Exception: ArcGISException: code=18004; A token or API key is required.; A token or API key was not provided to access https://arcgis.xyz.vn/arcgis/rest/services/xyz/MapServer/

0 Kudos
1 Solution

Accepted Solutions
JenMerritt
Esri Contributor

Actually re-reviewing your code, with the PregeneratedTokenCredential, this is slightly different to the TokenCredential workflow. Are you looking to have a user login experience in your app? If so, you can either use the TokenCredential or OAuthUserCredential workflows. For these you need to override the handleArcGISAuthenticationChallenge method.

If you are looking to use a pregenerated token, then this is for the workflow where you are using a token generated outside of your application. i.e. you can just get your pregenerated token and add this to the credential store - you just need to make sure you do this before you load the map or any secured services you are using.

ArcGISEnvironment.authenticationManager.arcGISCredentialStore.add(credential: pregeneratedTokenCredential);

 

View solution in original post

0 Kudos
3 Replies
JenMerritt
Esri Contributor

Hi @locho - thanks for sending your question through the forum 🙂

Hmm I wonder if this could be a timing issue if it's working sometimes and not others. May I ask what is it you are trying to load - a Portal, a type of layer, something else? And are you explicitly calling and awaiting load on whatever it is you are loading? Do you have any repro code from that part of the app?

Thanks - any other info you can provide may assist us in helping figure things out!

0 Kudos
locho
by
New Contributor

I have loaded ArcGIS MapImageLayer just like the sample code

https://developers.arcgis.com/flutter/sample-code/add-map-image-layer/

Thanks.

0 Kudos
JenMerritt
Esri Contributor

Actually re-reviewing your code, with the PregeneratedTokenCredential, this is slightly different to the TokenCredential workflow. Are you looking to have a user login experience in your app? If so, you can either use the TokenCredential or OAuthUserCredential workflows. For these you need to override the handleArcGISAuthenticationChallenge method.

If you are looking to use a pregenerated token, then this is for the workflow where you are using a token generated outside of your application. i.e. you can just get your pregenerated token and add this to the credential store - you just need to make sure you do this before you load the map or any secured services you are using.

ArcGISEnvironment.authenticationManager.arcGISCredentialStore.add(credential: pregeneratedTokenCredential);

 

0 Kudos