【问题标题】:Azure Devops Access with Azure AD client credential grant flowAzure Devops Access 与 Azure AD 客户端凭据授予流
【发布时间】:2018-11-26 17:49:58
【问题描述】:

我已成功通过 Azure AD 进行身份验证并收到访问令牌。我已授予 Azure AD App API 访问 Azure DevOps 的权限

我正在使用 RestSharp Http 客户端进行身份验证

var client = new RestClient("https://login.microsoftonline.com/{tenant}/oauth2/token");
            var request = new RestRequest("", Method.POST);
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", "00000f-0000-00-00-000000");
            request.AddParameter("client_secret", "][M.&*******?*_5z)y${*[)");
            request.AddParameter("resourse", "https://tenant.onmicrosoft.com/4815c06b-7e28-4f88-9dc8-8fe3354d5909");
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw con

到目前为止,我很高兴。我想不通的是如何使用访问令牌来访问 Azure DevOps 我迄今为止尝试过的

 var client = new RestClient("https://app.vssps.visualstudio.com/oauth2/token");
        var request = new RestRequest("", Method.POST);
        request.AddParameter("client_id", "My APP code here";
        request.AddParameter("client_secret", "My ap secret here");
        request.AddParameter("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
        request.AddParameter("client_assertion", "access token here");
        request.AddParameter("grant_type", "authorization_code");

我收到的错误是 "{\"Error\":\"unsupported_grant_type\",\"ErrorDescription\":\"grant_type 必须是 ietf jwt-bearer 类型或 refresh_token\"}"

我错过了什么?

【问题讨论】:

    标签: c# azure oauth-2.0 azure-devops azure-active-directory


    【解决方案1】:

    据我所知,不支持客户端凭据。 如果我们想使用 Azure AD 凭据访问 Azure Devops API,我们需要创建一个 Azure AD 本机应用程序并为 Azure AD 应用程序分配权限

    之后我们还需要connect the Azure Devepops account to the Azure AD.

    在浏览器中输入地址,输入用户名和密码即可获得授权码

    获取授权码

    https://login.microsoftonline.com/{tenantId}/oauth2/authorize?resource=499b84ac-1321-427f-aa17-267ca6975798&client_id={applicationId}&response_type=code&redirect_uri={redirecturl}
    

    获取访问令牌

    POST login.microsoftonline.com/e4c9ab4e-bd27-40d5-8459-230ba2a757fb/oauth2/token HTTP/1.1
    Content-Type: application/x-www-form-urlencoded
    Body
    resource=499b84ac-1321-427f-aa17-267ca6975798&client_id={clientid}&grant_type=authorization_code&code=AQABAAIAAA..&redirect_uri={redirectUrl}
    

    然后我们可以使用访问令牌来访问 Azure Devops API。

    我们可以从link 获得更多详细步骤和 C# 演示代码。

    【讨论】:

    • 请原谅我的无知,但我在哪里可以找到资源 ID
    • @user1021779 azure devops 的资源 ID 是 499b84ac-1321-427f-aa17-267ca6975798,无法更改。
    【解决方案2】:

    如果你有access_token,那么你可以使用access_token来访问web api资源,有一个示例供参考。

    GET /data HTTP/1.1
    Host: service.contoso.com
    Authorization: Bearer
    "your concrete access_token"
    

    您可以点击这个link查看详细信息,希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 1970-01-01
      • 2019-06-16
      • 2019-12-15
      • 2017-03-11
      • 2021-04-10
      • 2020-07-07
      • 2021-10-25
      • 1970-01-01
      相关资源
      最近更新 更多