使用 REST API 获取 Azure DevOps 中的所有组织

Get all organizations in Azure DevOps using REST API

我正在尝试检索我帐户中的所有组织,但在文档中,API 调用中始终需要一个组织。

https://dev.azure.com/{organization}/_apis/...

一个 REST API request/response 对可以分为五个部分:

请求URI,格式如下:

VERB https://{instance}[/{team-project}]/_apis[/{area}]/{resource}?api-version={version}

实例: 您要将请求发送到的 Azure DevOps 服务组织或 TFS 服务器。

它们的结构如下: Azure DevOps 服务:dev.azure.com/{organization}

REST API 是特定于组织的。目前没有记录。您可以在此处提交功能请求:https://developercommunity.visualstudio.com/spaces/21/index.html

我们的产品经理和产品团队会认真审核您的建议。不便之处,敬请谅解。

作为解决方法,您可以使用从网络流量中捕获的 API,正如 Matt 提到的那样。

如果您加载当前登录页面,它会显示与您的帐户相关联的所有组织。我认为它必须以某种方式获取该信息。我捕获了网络流量,我相信您可以使用系统 API 调用获得您想要的数据。但是,它可能会更改或不受支持,恕不另行通知,因此请自行决定使用。

您可以使用此 API:

获取您想要的信息
Post https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1

正文:

{
    "contributionIds": ["ms.vss-features.my-organizations-data-provider"],
    "dataProviderContext":
        {
            "properties":{}
        }
}

响应:

{
    "dataProviderSharedData": {},
    "dataProviders": {
        "ms.vss-web.component-data": {},
        "ms.vss-web.shared-data": null,
        "ms.vss-features.my-organizations-data-provider": {
            "organizations": [
                {
                    "id": "{redacted id}",
                    "name": "{organization1}",
                    "url": "https://{organization1}.visualstudio.com/"
                },
                {
                    "id": "{redacted id}",
                    "name": "{organization2}",
                    "url": "https://dev.azure.com/{organization2}/"
                }
            ],
            "createNewOrgUrl": "https://app.vsaex.visualstudio.com/go/signup?account=true"
        }
    } }

我们一直在使用“https://app.vssps.visualstudio.com/_apis/accounts”,但没有指定任何 API 版本,这 returns 我们所有的帐户名

这对我们仍然有效,但是由于我们遇到了一些其他问题,我正在将 api 版本添加到我们所有的 api 调用中。为此,我还 运行 了解 https://docs.microsoft.com/en-us/rest/api/azure/devops/account/accounts/list?view=azure-devops-rest-5.0 需要会员或所有者 ID 的事实。

检索需要一个 account/organization,所以这有点像第 22 条军规。

我想现在我将只使用“https://app.vssps.visualstudio.com/_apis/accounts

我收到了 "app.vssps.visualstudio.com/_apis/accounts" 和 Post https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1

状态码:203 StatusDescription : 非权威信息

编辑: 没关系,它使用静态 MSA clientid 和 replyURL:

internal const string clientId = "872cd9fa-d31f-45e0-9eab-6e460a02d1f1";          //change to your app registration's Application ID, unless you are an MSA backed account
            internal const string replyUri = "urn:ietf:wg:oauth:2.0:oob";                     //change to your app registration's reply URI, unless you are an MSA backed account

 //PromptBehavior.RefreshSession will enforce an authn prompt every time. NOTE: Auto will take your windows login state if possible
            result = ctx.AcquireTokenAsync(azureDevOpsResourceId, clientId, new Uri(replyUri), promptBehavior).Result;
            Console.WriteLine("Token expires on: " + result.ExpiresOn);

            var bearerAuthHeader = new AuthenticationHeaderValue("Bearer", result.AccessToken);

// Headers
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("User-Agent", "ManagedClientConsoleAppSample");
            client.DefaultRequestHeaders.Add("X-TFS-FedAuthRedirect", "Suppress");
            client.DefaultRequestHeaders.Authorization = authHeader;


            //Get Organizations
            client.BaseAddress = new Uri("https://app.vssps.visualstudio.com/");
            HttpResponseMessage response1 = client.GetAsync("_apis/accounts").Result;

您可以通过拨打电话获取您作为会员/所有者的所有帐户来轻松完成。但是,为此您需要您的 ID,可以通过调用 get profile 轻松获取它。以下是步骤:

  1. 调用 VSTS API 以使用 Bearer 令牌或 PAT 获取配置文件详细信息

https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=5.1

这将 return 你,你的 Id:

{
    "displayName": "xxxx",
    "publicAlias": "xxx",
    "emailAddress": "xxx",
    "coreRevision": xxx,
    "timeStamp": "2019-06-17T09:29:11.1917804+00:00",
    "id": "{{We need this}}",
    "revision": 298459751
}
  1. 接下来,拨打电话以获取您所属或所有者的所有帐户:

https://app.vssps.visualstudio.com/_apis/accounts?api-version=5.1&memberId={{Your 身份证}}

回复:

{
    "count": 1,
    "value": [
        {
            "accountId": "xxx",
            "accountUri": "xxx",
            "accountName": "xxx",
            "properties": {}
        }
    ]
}

它将 return 与您关联的帐户列表。