Jira API帮助C#HttpClient

问题描述 投票:0回答:1

好的,所以我对在代码中使用API​​感到很陌生,我已经能够使用实际上非常简单的一些API。但是它们都不要求认证。我一直在尝试通过C#的HttpClient类使用Jira的REST API服务。参见下面的代码:

public void UpdateJiraIssue(string issueValue)
        {
            string url = $@"http://jira.mySite.com/rest/api/2/issue/{issueValue}/editmeta";
            string jsonString = @"myNeatJsonData";
            var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

            //Initialize Client
            HttpClient apiClient = new HttpClient();
            apiClient.BaseAddress = new System.Uri(url);
            apiClient.DefaultRequestHeaders.Accept.Clear();

            byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
            apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
            apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            async Task RunJiraAPI()
            {

                using (HttpResponseMessage resp = await apiClient.PostAsync("editmeta", content))
                {
                    if (resp.IsSuccessStatusCode)
                    {
                        var jsonSring = await resp.Content.ReadAsStringAsync();
                    }
                }
            }
            RunJiraAPI();

            return;
        }

我遇到的问题是出现401错误(身份验证)。这是我运行代码时“ resp”对象包含的内容:

resp: {StatusCode: 401, ReasonPhrase: ' ', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-AREQUESTID: 400x1314x1
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  X-Frame-Options: SAMEORIGIN
  Content-Security-Policy: frame-ancestors 'self'
  X-ASEN: SEN-11158344
  X-AUSERNAME: anonymous
  Cache-Control: no-store, no-transform, no-cache
  Set-Cookie: atlassian.xsrf.token=B2ZY-C2JQ-1AGH-PBLW_5ccc79da5af8e6abcb9bff5250f3305af3b2877a_lout; Path=/; Secure
  WWW-Authenticate: OAuth realm="https%3A%2F%2Fjira.mySite.com"
  X-Powered-By: ARR/3.0
  X-Powered-By: ASP.NET
  Date: Wed, 15 Jan 2020 13:40:22 GMT
  Content-Length: 109
  Content-Type: application/json; charset=UTF-8
}}

Request Message: {Method: POST, RequestUri: 'https://jira.rhlan.com/rest/api/2/issue/RHD-1116/editmeta', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Authorization: Basic cWE6aGVjc29mdDEyMw==
  Accept: application/json
  Content-Type: Application/json; charset=utf-8
  Content-Length: 70
}}

Status Code: Unauthorized

我需要稍微处理一下json字符串才能使其正常工作(这就是为什么我不包括它实际包含的内容),但是一旦我通过了身份验证错误,我实际上可能会改变事情通过API获取Jira问题,因此我可以看到以这种方式返回的所有json数据。然后,我将相应地编辑我的json字符串。

任何想法我在这里做错了什么?

c# api jira dotnet-httpclient
1个回答
0
投票

假设您具有用户名和api令牌,您可以传递凭据。

string credentials= string.Format("{0}:{1}", username, apitoken);
    byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(credentials);

并且在apiClient中,您可以像这样使用它。

apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteCredentials));
© www.soinside.com 2019 - 2024. All rights reserved.