Azure devops 无法使用TFS 2015 API。获取未经验证的错误

Azure devops 无法使用TFS 2015 API。获取未经验证的错误,azure-devops,azure-devops-rest-api,azure-rest-api,Azure Devops,Azure Devops Rest Api,Azure Rest Api,我试过了 但是,它提到了请求对象(因为我不能使用javascript),不确定请求对象在哪里或者是什么类型的 我试图传递查询id,代码应该执行查询并通过API获得结果。 该解决方案可以从my local运行,但是,在发布到服务器后,它似乎不起作用。 我还检查了是否可以使用凭据从服务器访问TFS 我的代码如下: private HttpClientHandler GetTfsCredentials() { HttpClientHandler handler2 = n

我试过了 但是,它提到了请求对象(因为我不能使用javascript),不确定请求对象在哪里或者是什么类型的

我试图传递查询id,代码应该执行查询并通过API获得结果。 该解决方案可以从my local运行,但是,在发布到服务器后,它似乎不起作用。 我还检查了是否可以使用凭据从服务器访问TFS

我的代码如下:

    private HttpClientHandler GetTfsCredentials()
    {
        HttpClientHandler handler2 = new HttpClientHandler { UseDefaultCredentials = true };
        handler2.Credentials = new NetworkCredential("username", "password", "domain");
        return handler2;
    }
        private async Task<object> GetQueryResults(string queryId)
    {
        string tfsApiUrl = ConfigurationManager.AppSettings["TfsApiUrl"];
        string tfsProjectName = ConfigurationManager.AppSettings["TfsProjectName"];
        string TfsProjectGuid = ConfigurationManager.AppSettings["TfsProjectGuid"];

        //I tried both credentials and credentials2, but none of them working

        string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{""}:{"password"}"));

        string credentials2 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("domain\\username:password") );

        if (!string.IsNullOrEmpty(tfsApiUrl) && !string.IsNullOrEmpty(tfsProjectName)
            && !string.IsNullOrEmpty(Id))
        {
            log.Info("GetQueryResults:: Config values found");
            using (var client = new HttpClient(GetTfsCredentials()) { BaseAddress = new Uri(tfsApiUrl) })
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials2);

                HttpResponseMessage response = client.GetAsync($"{tfsProjectName}/_apis/wit/wiql/{Id}").Result;

                log.Info("GetQueryResults:: response.ReasonPhrase" + response.ReasonPhrase.ToString());
                log.Info("GetQueryResults:: response" + response.ToString());
                log.Info("GetQueryResults:: response.IsSuccessStatusCode" + response.IsSuccessStatusCode.ToString());
                string workItemList = null;

                if (response.IsSuccessStatusCode)
                {
                    //do something
                }
            }
        }

        return null;

    }

看起来您同时以两种不同的方式进行身份验证:

  • gettfscdentials
    -方法中设置Windows身份验证(NTLM或Kerberos)
  • 通过添加
    client.DefaultRequestHeaders.Authorization
    您可以尝试设置基本身份验证
您的TFS表明(请参见
WWW-Authenticate
Header)它支持承载、协商和NTLM;但不是基本的

我会尝试:

  • 删除
    client.DefaultRequestHeaders.Authorization
    凭证
    凭证2
    。这将删除基本身份验证
  • 删除
    UseDefaultCredentials=true
    ,因为您在下一行设置了显式凭据
    UseDefaultCredentials
    告诉
    HttpClientHandler
    使用正在运行的进程的凭据访问TFS,该进程在本地执行时可能是您的帐户,在服务器上执行时可能是服务帐户。
    在没有此行的情况下,应使用指定的
    NetworkCredential
    访问TFS

  • 这似乎有效,但现在我得到了另一个错误:NotFound,我直接在浏览器中尝试了裸URL,它确实返回了响应。不确定为什么通过API我得到NotFound错误。
    http://www.{orgname}/tfs/{DEPT}/{projectName}/_-API/wit/wiql/fd32daa7-ecdb-41f7-aa36-6736dfbbb792
    这是一种格式-在浏览器中工作,但不通过API。不要紧,这是权限问题,分配后可以修复。非常感谢。