How to update an issue status through C# code? Facing 401 Unauthorized error.

BHAWNA PARETA August 5, 2021

I am trying to update status of an issue in JIRA using C# code. I found various curl codes for the same, but since I am unfamiliar to curl, I converted the code to C#. 

Now, the first error I am facing is "Status code: 401", "Unauthorized". I am using email and API token to authorize. Searched and read through all the blogs/questions, found very similar issues but unable to figure out the issue with my code. 

Also I am new to C# and Jira environment, it would be great if you could help me out with finding the error. Or if I am missing something in the code?

See code below:

using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("PUT"), "https://<myhostname>.atlassian.net/rest/api/2/issue/<project>-<issueno>"))
{
var username = "<my-email>";
var password = "<API Token>";
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

request.Content = new StringContent("{\"transition\":{\"id\":\"11\"}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

var response = await httpClient.SendAsync(request);
Console.WriteLine(response);
}
}

 

Thanks

Bhawna

 

1 answer

1 accepted

1 vote
Answer accepted
BHAWNA PARETA August 6, 2021

I finally solved my issue of "401 Unauthorized" by changing my code to Encoding.UTF8.GetBytes but that alone didn't help me. Replacing "Basic" with "Bearer" in the following line did help me resolve 401 error.

request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events