The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

BHAWNA PARETA
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Answer accepted
BHAWNA PARETA
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
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}");
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events