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

使用Python Requests调用Azure DevOps API获取团队成员遇203认证问题求助

解决Azure DevOps API 203未认证问题的方案

核心原因

203状态码明确表示请求缺少有效认证凭据,Azure DevOps所有API接口均要求认证,官方文档的认证规则通常集中在通用认证章节,容易被忽略。

两种可行的认证实现方案(适用于Jupyter Notebook)

方案1:个人访问令牌(PAT)+ 基本认证(最常用)

这是调用Azure DevOps API的标准方式,步骤如下:

  1. 生成PAT:
    • 登录Azure DevOps门户,点击右上角头像 → 「安全」→ 「个人访问令牌」→ 「新建令牌」
    • 勾选Team Member相关权限(或按需选择更精细的权限),设置合理的有效期
  2. 编写认证请求代码:
    import requests
    
    # 替换为你的实际参数
    organization = "你的组织名称"
    project = "目标项目名称"
    team_id = "目标团队ID"
    pat = "你生成的个人访问令牌"
    
    # 构造API请求URL
    api_url = f"https://dev.azure.com/{organization}/{project}/_apis/teams/{team_id}/members?api-version=6.0&$expand=identity"
    
    # 基本认证:用户名留空,密码填入PAT
    response = requests.get(api_url, auth=("", pat))
    
    # 处理响应
    if response.status_code == 200:
        team_members = response.json()
        print(team_members)
    else:
        print(f"请求失败,状态码:{response.status_code},响应详情:{response.text}")
    

方案2:Azure AD令牌认证(企业级场景适用)

如果你的环境使用Azure AD管理身份,可通过MSAL库获取令牌完成认证:

  1. 安装依赖库:
    !pip install msal
    
  2. 编写认证请求代码:
    import requests
    import msal
    
    # 替换为你的Azure AD及Azure DevOps参数
    client_id = "你的Azure AD应用ID"
    tenant_id = "你的Azure AD租户ID"
    organization = "你的组织名称"
    project = "目标项目名称"
    team_id = "目标团队ID"
    
    # 初始化MSAL客户端
    msal_app = msal.PublicClientApplication(client_id, authority=f"https://login.microsoftonline.com/{tenant_id}")
    
    # 交互式获取令牌(Jupyter中会弹出登录窗口)
    token_result = msal_app.acquire_token_interactive(scopes=["499b84ac-1321-427f-aa17-267ca6975798/.default"])
    
    if "access_token" in token_result:
        api_url = f"https://dev.azure.com/{organization}/{project}/_apis/teams/{team_id}/members?api-version=6.0&$expand=identity"
        headers = {"Authorization": f"Bearer {token_result['access_token']}"}
        response = requests.get(api_url, headers=headers)
    
        if response.status_code == 200:
            team_members = response.json()
            print(team_members)
        else:
            print(f"请求失败,状态码:{response.status_code},响应详情:{response.text}")
    else:
        print(f"令牌获取失败:{token_result.get('error_description')}")
    

关键注意事项

  • PAT权限需匹配:至少需要Read级别的团队成员权限,避免因权限不足返回403
  • 确认参数正确性:team_id需从Azure DevOps团队设置页面获取,不能用团队名称替代
  • 先做小范围测试:可先调用获取项目列表的简单API,验证认证配置是否生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 14:31:51