Azure AI Studio调用带ML Index的GPT4部署API遇access_token无效错误求助
问题解决方案
一、获取Azure ML工作区的access_token方法
方法1:通过Azure CLI获取
- 安装并登录Azure CLI:
az login,按提示完成身份验证 - 执行命令获取令牌:
返回JSON中的az account get-access-token --resource https://ml.azure.comaccessToken字段即为所需令牌
方法2:通过服务主体获取(适合生产环境)
- 在Azure AD中注册应用程序,获取
tenant-id、client-id、client-secret - 执行以下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调用错误排查与修正
验证令牌权限
- 确保获取令牌的身份(用户或服务主体)拥有目标ML工作区的
Cognitive Services User或Machine Learning Data Scientist角色,可在Azure门户的ML工作区「访问控制(IAM)」中配置
- 确保获取令牌的身份(用户或服务主体)拥有目标ML工作区的
核对请求参数
- 确认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后必须有空格
- 确认API请求中
检查令牌有效期
- Azure AD令牌默认有效期1小时,过期后需重新获取;生产环境建议实现自动刷新逻辑
内容的提问来源于stack exchange,提问作者RazorKillBen
相关产品推荐
相关产品推荐

