Jira Service Desk API 调用从 c# Web 应用程序返回 401

Jira Service Desk API call returning 401 from c# web application

今天早上确实有效,现在 returns 出现 401 错误 (System.Net.HttpStatusCode.Unauthorized)。

另外,我不能再用Postman来测试了,同样是Unauthorized (401)。

但是,如果我登录到 Jira,然后打开一个新选项卡,我可以粘贴 api 调用和 poof,即时 json 响应。所以看起来凭据是正确的。

        var uriString = "https://journal.atlassian.net/rest/servicedeskapi/request?start=" + start.ToString() + "&limit=" + limit.ToString();
        var uri = new Uri(uriString);

        //var username = "my.email@someplace.com";
        //var password = "mypassword";

        var username = util.GetJiraAPIusername();
        var password = util.GetJiraAPIpassword();

        ViewBag.URI = uri;
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(uri).Result;  // Blocking call!
        if (response.IsSuccessStatusCode){ 

您是否尝试过检查您的请求 headers?我正在使用 Fiddler Web Debugger(您可以使用您想要的任何工具来执行此操作)拦截并记录所有 HTTP 请求。您应该会在 headers 您提出的请求中看到类似这样的内容:

更新:事实证明这是由于 Atlassian 方面的更改所致。一旦 Jira 管理员对我的帐户进行了必要的更改,我就可以再次登录并使用 Postman。

https://confluence.atlassian.com/cloudkb/jira-service-desk-cloud-rest-api-changes-to-atlassian-account-940700797.html

On 1 January, 2018, we're changing how you authenticate users with the Jira Service Desk Cloud REST API.

Right now, the REST API supports authentication for all users. However, on 1 January, users will need to have an Atlassian Account to authenticate against the REST API.

If users on your instance are authenticating against the REST API but do not have Atlassian Accounts, you'll need to convert them to Atlassian Account. Here's how:

  1. Go to Site administration > User management > Jira Service Desk > Portal only customers.
  2. Select the user you want to convert, then click Convert to Atlassian Account.
  3. Go to id.atlassian.com and log in with the user's email address.
  4. Go to Security > API tokens.
  5. Click Create API token.
  6. Copy the token and use it as you would a password for basic authentication. The username is the user's email address.