简体   繁体   English

Jira API 帮助 C# HttpClient

[英]Jira API Help C# HttpClient

Okay, so I'm very new to using API's in code and I've been able to use a few that were actually pretty easy.好的,所以我对在代码中使用 API 非常陌生,而且我已经能够使用一些实际上非常简单的方法。 But none of them required authentication.但它们都不需要身份验证。 I've been trying to use Jira's REST API service via C#'s HttpClient class.我一直在尝试通过 C# 的 HttpClient 类使用 Jira 的 REST API 服务。 See code below:见下面的代码:

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;
        }

The problem I run into is that I get a 401 error (Authentication).我遇到的问题是我收到 401 错误(身份验证)。 Here's what my 'resp' object contains when I run the code:这是我运行代码时我的“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

I need to work on my json string a bit to get it working right (which is why I didn't include what it actually contains), but once I get passed the authentication error, I'll probably actually change things to do a get Jira issue via the API so I can see all the json data returned that way.我需要稍微处理一下我的 json 字符串以使其正常工作(这就是为什么我没有包含它实际包含的内容),但是一旦我通过了身份验证错误,我可能实际上会改变一些事情来做一个 get Jira 通过 API 发出问题,因此我可以看到所有以这种方式返回的 json 数据。 Then I'll edit my json string accordingly.然后我将相应地编辑我的 json 字符串。

Any ideas what I'm doing wrong here?任何想法我在这里做错了什么?

You can pass in credentials assuming you have a username and an api token.假设您有用户名和 api 令牌,您可以传入凭据。

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

And in your apiClient you can use it like this.在您的 apiClient 中,您可以像这样使用它。

apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteCredentials));

You need a username and api-token.您需要一个用户名和 api 令牌。 Your api-token should be your login password.您的 api-token 应该是您的登录密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM