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
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}");
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.