C# urirequesturi=Authenticator.UriCombine(baseurl,featurePath); HttpResponseMessageResponse=client.GetAsync(requestUri).Result; 字符串结果=response.Content.ReadAsStringAsync().result; 结果=结果。替换(“系统”,“系统”); WorkItem=JsonConvert.DeserializeObject(结果); 添加(工作项); } } foreach(响应中的var项) { foreach(item.value.ToList()中的变量x) { WorkItemsToJsonFile(x); 结果列表。添加(x); } } 返回结果列表; }

C# urirequesturi=Authenticator.UriCombine(baseurl,featurePath); HttpResponseMessageResponse=client.GetAsync(requestUri).Result; 字符串结果=response.Content.ReadAsStringAsync().result; 结果=结果。替换(“系统”,“系统”); WorkItem=JsonConvert.DeserializeObject(结果); 添加(工作项); } } foreach(响应中的var项) { foreach(item.value.ToList()中的变量x) { WorkItemsToJsonFile(x); 结果列表。添加(x); } } 返回结果列表; },c#,oauth-2.0,azure-devops,azure-devops-rest-api,C#,Oauth 2.0,Azure Devops,Azure Devops Rest Api,使用固定登录更容易,不需要Oauth2尽管手动执行Oauth2并不难,只要将令牌转换为承载身份验证头…我想知道是否是这样,但是查看类需要的参数,我不知道应该传递什么值,而且看起来太复杂了。我有用户的访问令牌,当然我所需要做的就是将其包含在对API的请求中(正如这里“使用访问令牌”部分所暗示的:,尽管没有说明如何使用.NET客户端库)。我希望我刚开始使用REST API,但是当我发布我的问题时,我已经投资了.NET客户端库!我终于让它工作了——请看我发布的答案。另外,我很想使用OAuth,因为我将

使用固定登录更容易,不需要Oauth2尽管手动执行Oauth2并不难,只要将令牌转换为承载身份验证头…

我想知道是否是这样,但是查看类需要的参数,我不知道应该传递什么值,而且看起来太复杂了。我有用户的访问令牌,当然我所需要做的就是将其包含在对API的请求中(正如这里“使用访问令牌”部分所暗示的:,尽管没有说明如何使用.NET客户端库)。我希望我刚开始使用REST API,但是当我发布我的问题时,我已经投资了.NET客户端库!我终于让它工作了——请看我发布的答案。另外,我很想使用OAuth,因为我将有多个用户访问我的应用程序,我希望他们能够像自己一样从API读/写API。但是,如果您能告诉我如何在Ajax调用中成功地传递OAuth访问令牌(可以通过PAT来实现,但无法理解访问令牌),您将是我的英雄!从未听说过PAT tbh:(在C#中,使用HttpClient它是
HttpClient.DefaultRequestHeaders.Authorization=new AuthenticationHeaderValue(“承载人”,“您的Oauth令牌”);
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;
        }
    }
}
VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken)
VssOAuthAccessTokenCredential credentials = new VssOAuthAccessTokenCredential(AccessToken);
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;
        }