如何在nodejs和java中实现Azure Map getToken API?

问题描述 投票:0回答:1
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);
        }
    }
}

在nodejs和java中实现Azure Map getToken API

node.js azure-maps
1个回答
0
投票

按照以下步骤并参考 MSDOC 在 Node.js 中实现 Azure Map getToken API:

我已按照此 MSDOC 使用 JavaScript 管理 Microsoft Azure Maps 中的身份验证。

  • 添加 API 权限
    Access Azure Maps
    Sign in and read user profile
    ,并在您创建的注册应用程序中授予管理员同意。

enter image description here

  • Azure Maps Data Contributor
    角色添加到 Azure Maps 帐户中注册的应用程序。

enter image description here

下面的示例代码是如何获取 Azure Maps 访问令牌。

getAzureMapsBearerToken
函数使用 Node.js 中 Azure Identity 库的
ClientSecretCredential
类获取并打印令牌。

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

下面的代码是如何使用 MapsRoute 以及来自

ClientSecretCredential
的令牌访问 Azure Maps API。

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

使用 DefaultAzureCredential 类:

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

  • 按照此 MSDOC 使用 Java SDK 为 Azure Maps 应用程序生成令牌。
© www.soinside.com 2019 - 2024. All rights reserved.