【发布时间】:2019-02-19 09:04:54
【问题描述】:
我正在尝试检索我帐户中的所有组织,但在文档中,API 调用中始终需要一个组织。
https://dev.azure.com/{organization}/_apis/...
【问题讨论】:
标签: rest azure azure-devops azure-devops-rest-api
我正在尝试检索我帐户中的所有组织,但在文档中,API 调用中始终需要一个组织。
https://dev.azure.com/{organization}/_apis/...
【问题讨论】:
标签: rest azure azure-devops azure-devops-rest-api
如果您加载当前登录页面,它会显示与您的帐户相关联的所有组织。我认为它必须以某种方式获取该信息。我捕获了网络流量,我相信您可以使用系统 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"
}
} }
【讨论】:
您只需拨打电话即可获取您是会员/所有者的所有帐户。但是为此,您需要您的 id,可以通过调用 get profile 轻松获取。以下是以下步骤:
https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=5.1
这将返回您,您的 ID:
{
"displayName": "xxxx",
"publicAlias": "xxx",
"emailAddress": "xxx",
"coreRevision": xxx,
"timeStamp": "2019-06-17T09:29:11.1917804+00:00",
"id": "{{We need this}}",
"revision": 298459751
}
https://app.vssps.visualstudio.com/_apis/accounts?api-version=5.1&memberId={{YourId}}
回复:
{
"count": 1,
"value": [
{
"accountId": "xxx",
"accountUri": "xxx",
"accountName": "xxx",
"properties": {}
}
]
}
它将返回您关联的帐户列表。
【讨论】:
一个 REST API 请求/响应对可以分为五个部分:
请求URI,格式如下:
VERB https://{instance}[/{team-project}]/_apis[/{area}]/{resource}?api-version={version}
实例: 要将请求发送到的 Azure DevOps Services 组织或 TFS 服务器。
它们的结构如下:
Azure DevOps 服务:dev.azure.com/{organization}
REST API 是特定于组织的。目前没有记录。您可以在这里提交功能请求:https://developercommunity.visualstudio.com/spaces/21/index.html
我们的 PM 和产品团队会认真审核您的建议。给您带来不便,敬请见谅。
作为一种解决方法,您可以使用从网络流量中捕获的 API,就像 Matt 提到的那样。
【讨论】:
我们一直在使用“https://app.vssps.visualstudio.com/_apis/accounts”而不指定任何 API 版本,这会返回我们所有的帐户名
这仍然对我们有用,但是由于我们遇到的其他一些问题,我正在将 api 版本添加到我们所有的 api 调用中。为此,我还遇到https://docs.microsoft.com/en-us/rest/api/azure/devops/account/accounts/list?view=azure-devops-rest-5.0 需要成员或所有者 ID 的事实。
检索需要帐户/组织,因此有点像第 22 条问题。
【讨论】:
The remote server returned an error: (401) Unauthorized.。我正在使用 WebClient 和其他 AzDO REST API 请求的标准标头。
我收到了“app.vssps.visualstudio.com/_apis/accounts”和 发布https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1
状态码:203 StatusDescription : 非权威信息
编辑: 没关系,它使用静态 MSA 客户端 ID 和回复 URL:
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;
【讨论】: