You need to enable JavaScript to run this app. 如何使用@azure/msal-browser生成Kusto Azure Data Explorer作用域访问令牌
优惠活动
大模型
产品
解决方案
定价
更多

如何使用@azure/msal-browser生成Kusto Azure Data Explorer作用域访问令牌

解决MSAL获取Kusto访问令牌的配置方法

MSAL v2(@azure/msal-browser 及基于它封装的 @azure/msal-react)已经废弃了resource参数,所有资源的访问权限都通过scopes字段配置,你之前的配置方向是对的,没生效大多是细节配置遗漏导致,可按以下步骤修正:

正确代码示例

import { useMsal, useAccount } from '@azure/msal-react';
import { InteractionRequiredAuthError } from '@azure/msal-browser';

const ControllerRequester = () => {
  const { accounts, instance } = useMsal();
  const account = useAccount(accounts[0] || {});

  const getKustoToken = async () => {
    if (!account) return;
    const tokenRequest = {
      account,
      // 注意替换为你实际的Kusto集群地址,拼写必须完全匹配,不要多/少斜杠
      scopes: ['https://{name}.{location}.kusto.windows.net/.default']
    };

    try {
      // 静默获取令牌
      const tokenResponse = await instance.acquireTokenSilent(tokenRequest);
      // 注意:要取返回结果里的accessToken字段,不要直接用tokenResponse当令牌
      const accessToken = tokenResponse.accessToken;
      doSomething(accessToken);
    } catch (error) {
      // 静默获取失败时降级为交互获取
      if (error instanceof InteractionRequiredAuthError) {
        const tokenResponse = await instance.acquireTokenPopup(tokenRequest);
        const accessToken = tokenResponse.accessToken;
        doSomething(accessToken);
      } else {
        console.error('获取Kusto令牌失败:', error);
      }
    }
  };

  if (Object.keys(accounts).length) {
    getKustoToken();
  }

  return <div>...</div>
}

常见问题排查

  • 确认scopes里的Kusto地址拼写完全正确,集群名、区域和你实际使用的Kusto资源完全一致,末尾的/.default不能省略
  • 确认你MSAL初始化时使用的ClientID对应的AAD应用,已经在Azure门户中配置了Kusto服务的访问权限,且租户管理员已经完成权限同意
  • 确认MSAL初始化的authority配置正确,公有云环境格式为https://login.microsoftonline.com/[你的租户ID],不要填错租户信息

内容的提问来源于stack exchange,提问作者Alex Mckay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 10:06:08