如何使用授权例程提供的 VSTS OAuth2 Bearer Token?
How do I use the VSTS OAuth2 Bearer Token provided by the authorization routine?
我正在编写一个 MVC5 Web 应用程序以允许我在 Visual Studio Team Services (VSTS) 中查询我们的工作项。
遵循 this one and this one I have successfully created the application so that it will retrieve the Work Items I want using a Personal Access Token (PAT) that I created for the purposes of development. I have also, by closely following the sample available here 等教程后,成功创建了整个 OAuth2 流程,以便将用户带到 VSTS,要求授权我的应用程序,然后返回到我的回调页面。回调 URL 正确地包含用户的访问令牌、刷新令牌等。到目前为止,还不错。
我将用户的刷新令牌连同到期日期和时间一起存储在我的数据库中的用户记录中(以便我知道如果他们在访问令牌过期后尝试访问应用程序,我会刷新令牌)。
我的问题是我不知道如何在 C# 代码中使用用户的访问令牌而不是我自己的 PAT 来查询 VSTS。我正在使用的代码如下(它实际上与我在上面链接的 GitHub 上的示例中的代码相同),并且工作正常,但正如您所看到的,它使用的是 PAT。我该如何改为使用用户的访问令牌,目前我只是在 API 返回时将其视为 string
(可能是错误的?)。
public class GetFeatures
{
readonly string _uri;
readonly string _personalAccessToken;
readonly string _project;
public GetFeatures()
{
_uri = "https://myaccount.visualstudio.com";
_personalAccessToken = "abc123xyz456"; //Obviously I've redacted my actual PAT
_project = "My Project";
}
public List<VSTSFeatureModel> AllFeatures()
{
Uri uri = new Uri(_uri);
string personalAccessToken = _personalAccessToken;
string project = _project;
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
//create a wiql object and build our query
Wiql wiql = new Wiql()
{
Query = "Select [State], [Title] " +
"From WorkItems " +
"Where [Work Item Type] = 'Feature' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Removed' " +
"Order By [State] Asc, [Changed Date] Desc"
};
//create instance of work item tracking http client
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
{
//execute the query to get the list of work items in the results
WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;
//some error handling
if (workItemQueryResult.WorkItems.Count() != 0)
{
//...do stuff
}
return null;
}
}
}
您需要使用 VssOAuthCredential
而不是 VssBasicCredential
。
经过大量的猜测,由于 VSTS .NET 客户端库的文档非常少,我发现 VssOAuthCredential
似乎已被弃用。通过替换
,我能够使上面的代码示例正常工作
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken)
与
VssOAuthAccessTokenCredential credentials = new VssOAuthAccessTokenCredential(AccessToken);
其中 AccessToken 是一个 string
包含用户的 OAuth 访问令牌。
那个框架好像没什么用,哈哈...
我目前正在做一个备份 VSTS 帐户的项目,我正在通过 HttpRequests 到 REST API 来完成所有这一切。
public List<int> GetItemIDs()
{
HttpClient client = auth.AuthenticateHTTP(new HttpClient());
string content = $@"{{""query"": ""Select[System.Id] From WorkItems order by[System.CreatedDate] desc"" }}";
StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json");
string endpoint = "DefaultCollection/_apis/wit/wiql?api-version=1.0";
Uri requesturl = UriCombine(baseurl, endpoint);
HttpResponseMessage response = client.PostAsync(requesturl, stringContent).Result;
string result = response.Content.ReadAsStringAsync().Result;
var json = Newtonsoft.Json.JsonConvert.DeserializeObject<QueryResponse>(result);
return json.workItems.Select(x => x.id).ToList();
}
public List<string> ListToString200(List<int> ids) //Writes all IDs into comma seperated strings of up to 200 IDs and puts them into a List.
{
List<string> idStrings = new List<string>();
if (ids.Count > 200)
{
while (ids.Count > 200)
{
List<int> t = new List<int>();
var IDs = ids.Take(200);
ids.Remove(200);
foreach (var item in IDs)
{
t.Add(item);
}
var ID = t.ConvertAll(element => element.ToString()).Aggregate((a, b) => $"{a},{b}");
idStrings.Add(ID);
}
}
else if (ids.Count > 0)
{
var ID = ids.ConvertAll(element => element.ToString()).Aggregate((a, b) => $"{a}, {b}");
idStrings.Add(ID);
}
return idStrings;
}
private List<WorkItem> GetAllWorkItems()
{
List<int> ids = GetItemIDs();
List<WorkItemsContainer> Responses = new List<WorkItemsContainer>();
List<WorkItem> ResultList = new List<WorkItem>();
List<string> idStrings = ListToString200(ids);
using (HttpClient client = new HttpClient())
{
auth.AuthenticateHTTP(client);
foreach (var item in idStrings)
{
WorkItemsContainer WorkItem = new WorkItemsContainer();
string featurePath = $"DefaultCollection/_apis/wit/workitems?ids={item}&$expand=all&api-version=1.0";
Uri requestUri = Authenticator.UriCombine(baseurl, featurePath);
HttpResponseMessage response = client.GetAsync(requestUri).Result;
string result = response.Content.ReadAsStringAsync().Result;
result = result.Replace("System.", "System");
WorkItem = JsonConvert.DeserializeObject<WorkItemsContainer>(result);
Responses.Add(WorkItem);
}
}
foreach (var item in Responses)
{
foreach (var x in item.value.ToList<WorkItem>())
{
WorkItemsToJsonFile(x);
ResultList.Add(x);
}
}
return ResultList;
}
使用固定登录更容易,虽然不需要 Oauth2,但手动执行 OAuth2 并没有那么难,只需将令牌变成不记名身份验证 header...
我正在编写一个 MVC5 Web 应用程序以允许我在 Visual Studio Team Services (VSTS) 中查询我们的工作项。
遵循 this one and this one I have successfully created the application so that it will retrieve the Work Items I want using a Personal Access Token (PAT) that I created for the purposes of development. I have also, by closely following the sample available here 等教程后,成功创建了整个 OAuth2 流程,以便将用户带到 VSTS,要求授权我的应用程序,然后返回到我的回调页面。回调 URL 正确地包含用户的访问令牌、刷新令牌等。到目前为止,还不错。
我将用户的刷新令牌连同到期日期和时间一起存储在我的数据库中的用户记录中(以便我知道如果他们在访问令牌过期后尝试访问应用程序,我会刷新令牌)。
我的问题是我不知道如何在 C# 代码中使用用户的访问令牌而不是我自己的 PAT 来查询 VSTS。我正在使用的代码如下(它实际上与我在上面链接的 GitHub 上的示例中的代码相同),并且工作正常,但正如您所看到的,它使用的是 PAT。我该如何改为使用用户的访问令牌,目前我只是在 API 返回时将其视为 string
(可能是错误的?)。
public class GetFeatures
{
readonly string _uri;
readonly string _personalAccessToken;
readonly string _project;
public GetFeatures()
{
_uri = "https://myaccount.visualstudio.com";
_personalAccessToken = "abc123xyz456"; //Obviously I've redacted my actual PAT
_project = "My Project";
}
public List<VSTSFeatureModel> AllFeatures()
{
Uri uri = new Uri(_uri);
string personalAccessToken = _personalAccessToken;
string project = _project;
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);
//create a wiql object and build our query
Wiql wiql = new Wiql()
{
Query = "Select [State], [Title] " +
"From WorkItems " +
"Where [Work Item Type] = 'Feature' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Removed' " +
"Order By [State] Asc, [Changed Date] Desc"
};
//create instance of work item tracking http client
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
{
//execute the query to get the list of work items in the results
WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;
//some error handling
if (workItemQueryResult.WorkItems.Count() != 0)
{
//...do stuff
}
return null;
}
}
}
您需要使用 VssOAuthCredential
而不是 VssBasicCredential
。
经过大量的猜测,由于 VSTS .NET 客户端库的文档非常少,我发现 VssOAuthCredential
似乎已被弃用。通过替换
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken)
与
VssOAuthAccessTokenCredential credentials = new VssOAuthAccessTokenCredential(AccessToken);
其中 AccessToken 是一个 string
包含用户的 OAuth 访问令牌。
那个框架好像没什么用,哈哈...
我目前正在做一个备份 VSTS 帐户的项目,我正在通过 HttpRequests 到 REST API 来完成所有这一切。
public List<int> GetItemIDs()
{
HttpClient client = auth.AuthenticateHTTP(new HttpClient());
string content = $@"{{""query"": ""Select[System.Id] From WorkItems order by[System.CreatedDate] desc"" }}";
StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json");
string endpoint = "DefaultCollection/_apis/wit/wiql?api-version=1.0";
Uri requesturl = UriCombine(baseurl, endpoint);
HttpResponseMessage response = client.PostAsync(requesturl, stringContent).Result;
string result = response.Content.ReadAsStringAsync().Result;
var json = Newtonsoft.Json.JsonConvert.DeserializeObject<QueryResponse>(result);
return json.workItems.Select(x => x.id).ToList();
}
public List<string> ListToString200(List<int> ids) //Writes all IDs into comma seperated strings of up to 200 IDs and puts them into a List.
{
List<string> idStrings = new List<string>();
if (ids.Count > 200)
{
while (ids.Count > 200)
{
List<int> t = new List<int>();
var IDs = ids.Take(200);
ids.Remove(200);
foreach (var item in IDs)
{
t.Add(item);
}
var ID = t.ConvertAll(element => element.ToString()).Aggregate((a, b) => $"{a},{b}");
idStrings.Add(ID);
}
}
else if (ids.Count > 0)
{
var ID = ids.ConvertAll(element => element.ToString()).Aggregate((a, b) => $"{a}, {b}");
idStrings.Add(ID);
}
return idStrings;
}
private List<WorkItem> GetAllWorkItems()
{
List<int> ids = GetItemIDs();
List<WorkItemsContainer> Responses = new List<WorkItemsContainer>();
List<WorkItem> ResultList = new List<WorkItem>();
List<string> idStrings = ListToString200(ids);
using (HttpClient client = new HttpClient())
{
auth.AuthenticateHTTP(client);
foreach (var item in idStrings)
{
WorkItemsContainer WorkItem = new WorkItemsContainer();
string featurePath = $"DefaultCollection/_apis/wit/workitems?ids={item}&$expand=all&api-version=1.0";
Uri requestUri = Authenticator.UriCombine(baseurl, featurePath);
HttpResponseMessage response = client.GetAsync(requestUri).Result;
string result = response.Content.ReadAsStringAsync().Result;
result = result.Replace("System.", "System");
WorkItem = JsonConvert.DeserializeObject<WorkItemsContainer>(result);
Responses.Add(WorkItem);
}
}
foreach (var item in Responses)
{
foreach (var x in item.value.ToList<WorkItem>())
{
WorkItemsToJsonFile(x);
ResultList.Add(x);
}
}
return ResultList;
}
使用固定登录更容易,虽然不需要 Oauth2,但手动执行 OAuth2 并没有那么难,只需将令牌变成不记名身份验证 header...