问题描述
我目前正在尝试使用 Visual Studio Team Services(以前是 Visual Studio Online)公开的 Rest API 来获取工作项信息.然而,当我查看对我的查询的响应时,我似乎能够连接到一个带有增强安全错误消息的 html 页面.我相信这是由于 IE 中的增强安全选项造成的,但我是从我的客户端机器调用它,我只能看到有关如何在服务器上关闭它的选项.
这是我打的电话
using (var client = new HttpClient()){var token = "xxxxxxxxxxxx";var apiVersion = "1.0";var account = "xxxxxxxx";var query = "Select [System.Id] From WorkItems Where[System.WorkItemType] = 'WorkItem' order by [System.CreatedDate] desc";var url = "https://" + account + ".visualstudio.com/Core/_apis/wit/";//执行返回符合指定条件的工作项 ID 的查询使用 (var request = new HttpRequestMessage(HttpMethod.Post, url + "wiql")){request.Headers.Add("Authorization", "Bearer " + token);request.Headers.Add("Accept", "application/json;api-version=" + apiVersion);字典body = new Dictionary{{查询",查询}};request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");使用 (var response = await client.SendAsync(request)){var content = await response.Content.ReadAsStringAsync();var workItems = JObject.Parse(content)["workItems"] as JArray;string[] ids = workItems.Select(w => (w["id"] + "")).Take(10).ToArray();string idsString = String.Join(",", ids);//获取最近 10 个的详细信息使用 (var detailsRequest = new HttpRequestMessage(HttpMethod.Get, url + "workitems?ids=" + idsString + "&fields=System.Id,System.Title")){detailsRequest.Headers.Add("Authorization", "Bearer " + token);detailsRequest.Headers.Add("Accept", "application/json;api-version=" + apiVersion);使用 (var detailsResponse = await client.SendAsync(detailsRequest)){var detailsContent = await detailsResponse.Content.ReadAsStringAsync();var detailsWorkItems = JObject.Parse(detailsContent)["value"] as JArray;foreach(详细信息WorkItems中的动态工作项){Console.WriteLine("工作项:{0} ({1})",workItem.fields["System.Id"],workItem.fields["System.Title"]);}}}}}}
对此的任何帮助将不胜感激,
谢谢
克里斯
您可以将相关站点添加到受信任站点(例如:
更新:
根据响应结果,它与增强安全性无关,结果意味着它未通过身份验证.所以令牌无效,它是OAuth的访问令牌,您需要在将您的应用程序注册到VSTS后获取访问令牌.
更多信息,可以参考这个 文章.
您可以参考的 OAuth 示例.获取访问令牌后,将其添加到请求标头并从 VSTS 检索数据.
如果你想通过个人访问令牌访问VSTS,代码如下:(查看这篇 文章)
试试{var 用户名 = "用户名";var password = "密码";使用 (HttpClient 客户端 = 新 HttpClient()){client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", 用户名, 密码))));使用 (HttpResponseMessage 响应 = client.GetAsync("https://{account}.visualstudio.com/DefaultCollection/_apis/build/builds").结果){response.EnsureSuccessStatusCode();string responseBody = await response.Content.ReadAsStringAsync();Console.WriteLine(responseBody);}}}捕获(异常前){Console.WriteLine(ex.ToString());}
I'm currently trying to use the Rest APIs exposed by Visual Studio Team Services (was Visual Studio Online) to obtain work item information. I seem to be able to connect however when I look at the response to my query its a html page with a Enhanced Security Error message. I believe that this is due to the Enhanced Security option in IE but I'm calling this from my client machine and I can only see options on how to turn this off on a server.
this is the call i'm making
using (var client = new HttpClient())
{
var token = "xxxxxxxxxxxx";
var apiVersion = "1.0";
var account = "xxxxxxxx";
var query = "Select [System.Id] From WorkItems Where[System.WorkItemType] = 'WorkItem' order by [System.CreatedDate] desc";
var url = "https://" + account + ".visualstudio.com/Core/_apis/wit/";
// Execute a query that returns work item IDs matching the specified criteria
using (var request = new HttpRequestMessage(HttpMethod.Post, url + "wiql"))
{
request.Headers.Add("Authorization", "Bearer " + token);
request.Headers.Add("Accept", "application/json;api-version=" + apiVersion);
Dictionary<string, string> body = new Dictionary<string, string>
{
{
"query", query
}
};
request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
using (var response = await client.SendAsync(request))
{
var content = await response.Content.ReadAsStringAsync();
var workItems = JObject.Parse(content)["workItems"] as JArray;
string[] ids = workItems.Select<JToken, string>(w => (w["id"] + "")).Take(10).ToArray<string>();
string idsString = String.Join(",", ids);
// Get details for the last 10
using (var detailsRequest = new HttpRequestMessage(HttpMethod.Get, url + "workitems?ids=" + idsString + "&fields=System.Id,System.Title"))
{
detailsRequest.Headers.Add("Authorization", "Bearer " + token);
detailsRequest.Headers.Add("Accept", "application/json;api-version=" + apiVersion);
using (var detailsResponse = await client.SendAsync(detailsRequest))
{
var detailsContent = await detailsResponse.Content.ReadAsStringAsync();
var detailsWorkItems = JObject.Parse(detailsContent)["value"] as JArray;
foreach (dynamic workItem in detailsWorkItems)
{
Console.WriteLine("Work item: {0} ({1})",
workItem.fields["System.Id"],
workItem.fields["System.Title"]
);
}
}
}
}
}
}
any help with this would be appreciated,
thanks
Chris
You can add related sites to trusted sites (for example: https://app.vssps.visualstudio.com, https://login.live.com etc…).
- Internet option=>Security
- Select Trusted sites
- Click sites
- Type website address and click add
The simple way to know which URLs need to be added, you could send a simple Get Rest request (e.g. get work item REST API), it will pop up a window that contains site URL (will pop up many times for different URL), add these URL to trusted sites list.
Update:
Based on the response result, it isn’t related to enhanced security, the result means it isn’t authenticated. So the token is invalid, it is access token of OAuth, you need to get access token after register your app to VSTS.
More information, you can refer to this article.
There is a OAuth sample that you can refer. After you get access token, add it to request header and retrieve data from VSTS.
If you want to access VSTS through personal access token, the code like this: (check this article)
try
{
var username = "username";
var password = "password";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
using (HttpResponseMessage response = client.GetAsync(
"https://{account}.visualstudio.com/DefaultCollection/_apis/build/builds").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
这篇关于Visual Studio Team Services Rest API 时增强的安全性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!