You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AI Studio调用带ML Index的GPT4部署API遇access_token无效错误求助

问题解决方案

一、获取Azure ML工作区的access_token方法

方法1:通过Azure CLI获取

  1. 安装并登录Azure CLI:az login,按提示完成身份验证
  2. 执行命令获取令牌:
    az account get-access-token --resource https://ml.azure.com
    
    返回JSON中的accessToken字段即为所需令牌

方法2:通过服务主体获取(适合生产环境)

  1. 在Azure AD中注册应用程序,获取tenant-id、client-id、client-secret
  2. 执行以下curl命令获取令牌:
    curl -X POST https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "client_id={client-id}" \
    -d "scope=https://ml.azure.com/.default" \
    -d "client_secret={client-secret}" \
    -d "grant_type=client_credentials"
    
    返回结果中的access_token字段即为有效令牌

二、API调用错误排查与修正

  1. 验证令牌权限

    • 确保获取令牌的身份(用户或服务主体)拥有目标ML工作区的Cognitive Services User或Machine Learning Data Scientist角色,可在Azure门户的ML工作区「访问控制(IAM)」中配置
  2. 核对请求参数

    • 确认API请求中data_sources里的ML Index资源ID格式正确,示例:
      "data_sources": [
        {
          "type": "azure_ml_index",
          "parameters": {
            "index_name": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.MachineLearningServices/workspaces/xxx/indices/xxx"
          }
        }
      ]
      
    • 确保请求头的Authorization格式为Bearer {your-access-token},注意Bearer后必须有空格
  3. 检查令牌有效期

    • Azure AD令牌默认有效期1小时,过期后需重新获取;生产环境建议实现自动刷新逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 18:52:09