QML ArcGIS Runtime API - generate a new token

2533
15
Jump to solution
09-14-2022 06:13 AM
Labels (2)
GISiste
Regular Contributor

Hello everyone, 

I'm actually working on app mobile (doing some basics functionnalities like displaying data and searching the itineraire). The data is a simple web map with a featurelayer stored in the portal federated with the ArcGIS Server

The user can use her enterprise account to access to the portal then get the token to display the web map and the data. The token has 30 minutes to expire.

Now, I would like to generate a new token once the old one has expired without displaying the popup authentication. 

I am a junior in QT and ArcGIS Runtime API for QML and any advice is the most welcomed

 

Thank you for the help

0 Kudos
1 Solution

Accepted Solutions
DavidPuckett
Regular Contributor

Following up to learn if there is more info here. I have since implemented a solution that refreshes the token via a NetworkRequest put on a Timer which works most of the time. However, if the app idles too long in the background the timer will not fire to refresh the access token and the app loses access. This is especially difficult because there is no indication to the user at all. It just doesn't work and there isn't then a way to refresh the token unless I implement a manual way to refresh, inelegant. Any word on whether the refreshTokenExchangeInterval workflow has a workaround here @LucasDanzinger ?

View solution in original post

0 Kudos
15 Replies
LucasDanzinger
Esri Frequent Contributor

Have you seen the token authentication sample? https://github.com/Esri/arcgis-runtime-samples-qt/tree/main/ArcGISRuntimeSDKQt_QMLSamples/CloudAndPo...

 

This makes use of the ArcGIS Runtime Toolkit's AuthenticationView. This component will ask the user to sign in on load, but then will automatically exchange/refresh tokens behind the scenes.

We have other samples as well that showcase how to use OAuth, IWA, and other sign in options.

GISiste
Regular Contributor

Thank you @LucasDanzinger for your response. I've tested the sample and looks working. I will integrate this into my app and let you know

Thanks again

0 Kudos
GISiste
Regular Contributor

Hi @LucasDanzinger I've integrated this sample and the authentication popup still displaying when the token is expired.

0 Kudos
GISiste
Regular Contributor

Following up ... is there anyone who had an idea ?🤔

Thanks

0 Kudos
LucasDanzinger
Esri Frequent Contributor

As far as I know, the username and password supplied by the user should be used to obtain the new token once the old one expires. If that is not the case, then that is a bug in the API.

 

I have another suggestion to try:

 

Instead of using the AuthenticationView, you could connect to the AuthenticationManager and respond to credential challenges there. You can intercept the challenge and respond to the challenge programmatically 

Something like:

Connections {
    target: AuthenticationManager
    function onAuthenticationChallenge(challenge) {
        const credential = ArcGISRuntimeEnvironment.createObject("Credential", {username: "", password: ""});
        challenge.continueWithCredential(credential);
    }
}

 

Here is the doc on that https://developers.arcgis.com/qt/qml/api-reference/qml-esri-arcgisruntime-authenticationmanager.html

GISiste
Regular Contributor

thanks @ LucasDanzinger for youre help. I've tested the suggestion but still get the popup. Then I think this could be a bug in the API.

Hope this will be corrected soon

0 Kudos
DavidPuckett
Regular Contributor

So @LucasDanzinger if using oAuth, could this example be as simple as using the existing credential's refreshToken to create a new Credential and pass that to challenge.continueWithCredential(credential)?

I'm trying to refresh a token without prompting the user to log in again. Or, in my case, the app just hangs and 'acts weird' when the token expires. Thanks for any help.

0 Kudos
LucasDanzinger
Esri Frequent Contributor

If using OAuth, could you try setting the following OAuthClientInfo property: 

oAuthClientInfo: OAuthClientInfo {
    oAuthMode: Enums.OAuthModeUser
    clientId: appPortal.clientId
    refreshTokenExchangeInterval: 30 // <--- Add this
}

The API should use the refresh token to fetch a new access token for you automatically.

0 Kudos
DavidPuckett
Regular Contributor

Wow. Even easier. I actually had this commented out so you're telling me it would have automatically refreshed the token all along if enabled? 🙄 If this is the expected behavior then I will assume it works. I will report back otherwise. Thanks!

0 Kudos