Search code examples
node.jsazureazure-maps

How to implement the Azure Map getToken API in nodejs and java?


let Options = {
    authOptions: {
        authType: AuthenticationType.anonymous,
        clientId: ClientId,
        getToken: async (resolve: any, reject: any) => {
// How to implement the Azure Map getToken API in nodejs and java?
            resolve(bearerToken);
        }
    }
}

implement the Azure Map getToken API in nodejs and java


Solution

  • Follow the steps below and reference of MSDOC to implement the Azure Map getToken API in Node.js:

    I have followed this MSDOC to manage authentication in Microsoft Azure Maps using JavaScript.

    • Add API permissions Access Azure Maps and Sign in and read user profile, and grant admin consent in the registered application you created.

    enter image description here

    • Add the Azure Maps Data Contributor role to the registered application in the Azure Maps account.

    enter image description here

    The sample code below is to how to get an Azure Maps access token . The getAzureMapsBearerToken function fetches and prints the token using the ClientSecretCredential class of the Azure Identity library in Node.js.

    const { ClientSecretCredential } = require("@azure/identity");
    const azureClientId = process.env.AZURE_CLIENT_ID;
    const azureClientSecret = process.env.AZURE_CLIENT_SECRET;
    const azureTenantId = process.env.AZURE_TENANT_ID;
    
    const credential = new ClientSecretCredential(azureTenantId, azureClientId, azureClientSecret);
    // Example usage: Fetching a bearer token for Azure Maps
    async function getAzureMapsBearerToken() {
        try {
            // Fetching the token
            const token = await credential.getToken(["https://atlas.microsoft.com/.default"]);
    
            console.log("Access token:", token.token);
        } catch (error) {
            console.error("Failed to obtain Azure Maps token:", error);
        }
    }
    getAzureMapsBearerToken();
    
    
    

    enter image description here

    The code below is to how to access Azure Maps APIs using MapsRoute with a token from ClientSecretCredential.

    const { ClientSecretCredential } = require("@azure/identity");
    const MapsRoute = require("@azure-rest/maps-route").default;
    
    const azureClientId = process.env.AZURE_CLIENT_ID;
    const azureClientSecret = process.env.AZURE_CLIENT_SECRET;
    const azureTenantId = process.env.AZURE_TENANT_ID;
    const mapsAccountClientId = "64a376de-528d-4550-b161-9f8011329322"; // Replace with your Maps account client ID
    
    const credential = new ClientSecretCredential(azureTenantId, azureClientId, azureClientSecret);
    const client = MapsRoute(credential, mapsAccountClientId);
    
    async function getAzureMapsBearerToken() {
        try {
            // Fetching the token
            const token = await credential.getToken(["https://atlas.microsoft.com/.default"]);
    
            console.log("Access token:", token.token);
            
            const routeDirectionsResult = await client.path("/route/directions/{format}", "json").get({
                queryParameters: {
                    query: "51.368752,-0.118332:41.385426,-0.128929",
                },
            });
    
            console.log("Route directions result:", routeDirectionsResult.body);
        } catch (error) {
            console.error("Failed to obtain Azure Maps token:", error);
        }
    }
    
    getAzureMapsBearerToken();
    
    
    

    enter image description here

    Using DefaultAzureCredential Class:

    const { DefaultAzureCredential } = require("@azure/identity");
    
    async function getToken() {
        try {
            const credential = new DefaultAzureCredential();
            const tokenResponse = await credential.getToken("https://atlas.microsoft.com/.default");
            
          
            console.log("Azure Maps token:", tokenResponse.token);
            
            return tokenResponse.token;
        } catch (error) {
            console.error("Failed to get Azure Maps token:", error);
            throw error;
        }
    }
    async function main() {
        try {
            
            const bearerToken = await getToken();
           
            console.log("Token received:", bearerToken);
        } catch (error) {
            console.error("Error:", error);
        }
    }
    
    main();
    
    

    enter image description here

    enter image description here

    enter image description here

    • Follow this MSDOC for token generation for Azure Maps applications using the Java SDK.