Azure Map token using endpoint - call api again at token expiration?

Anisha KC 125 Reputation points Microsoft Employee
2023-09-26T18:12:26.36+00:00

Hello,

I am using React Azure Maps package: https://github.com/Azure/react-azure-maps

I am using the anonymous method mentioned here: https://learn.microsoft.com/en-us/azure/azure-maps/how-to-secure-webapp-users

I am getting this error if the user keeps the window open for a long time: Error: Token Expired, Try again at dh.getToken (...) at dh.signRequest (...) at j.transformRequest [as _transformRequestFn] (...) at e.exports.j.transformRequest (...) at e.exports.J.loadTile (...) at e.exports.ca._loadTile (...) at e.exports.ca._addTile (...) at e.exports.ca._updateRetainedTiles (...) at e.exports.ca.update (...) at ca.<anonymous> (...)

I had a quick question. If the token were to expire, would the SDK automatically call the endpoint again? If not, is there a way for me to handle expired tokens such that I can ask for another token?

Below are some code samples if it helps. The token URL is an endpoint of mine that returns a token for Azure Maps. Thank you!

const option: IAzureMapOptions = {
        authOptions: {
            authType: AuthenticationType.anonymous,
            clientId: {myclientid},
            aadTenant: {mytenantid},
            getToken: (resolve, reject, map) => {
                myapiclient.get(mytokenurl)
                .then(token => resolve(token.data))
                .catch(err => reject(err));
            }
        },
        showFeedbackLink: false,
        language: 'en-US'
    }

..........

<AzureMap options={option} cameraOptions={myCameraOptions} controls={myControls}>
Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
833 questions
{count} vote

Accepted answer
  1. rbrundritt 20,836 Reputation points Microsoft Employee Moderator
    2023-09-26T19:59:33.5666667+00:00

    The map is supposed to retrieve a new token when the current one expires. I know it used to, haven't tested that out myself recently and I believe the backend library used for this in Azure Maps was replaced about a year ago as the Azure Active Directory team retired the older version of it, so it's possible the behavior has changed. I believe the token is meant to last around 10 to 12 hours, the expiry time is encoded into the JWT token (I just verified this by grabbing the token from my network trace and decoding it using this tool: https://jwt.io/).

    Looking at the docs, the Event manager of the map has a tokenaquired event but not one for when it expires. Digging into the source code of the web SDK it doesn't look like the Azure Active Directory tokens have expiry handling setup, but SAS tokens do.

    I'll reach out to the Azure Maps team about this.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. rbrundritt 20,836 Reputation points Microsoft Employee Moderator
    2023-10-10T20:48:11.1433333+00:00

    Can you try using the latest version of the Azure Maps React library, and change the auth type to use Azure Active Directory:

    authOptions: {
      authType: AuthenticationType.aad,
      clientId: "...",
      aadAppId: "...",
      aadTenant: "..."
    }
    

    The AuthenticationType.aad auth type automatically gets a new token when needed.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.