Creating Rest Client with API Token in C#

Using the following example code:

var jira = Jira.CreateRestClient(“http://myurl.atlassian.net”, “email@address.com”, “p@ssw0rd”);
var issue = jira.CreateIssue(“Test Project”);
issue.Type = “Bug”;
issue.Priority = “Major”;
issue.Summary = “Issue Summary”;
await issue.SaveChangesAsync();

Throws a 401 unauthorized with a valid logged in user. I generated an API Token, but could not find any examples of using this with a C# application.

Any “working” example in C#, would be much appreciated.

Hi @RichardHall, have you also tried to use the API token instead of your password? Simply replace it and try it again.

Hi @anon28347317:

That was it, I used the api token and adjusted code and got working, thanks. Below is example method.

public async Task CreateJiraIssueAsync()
{
string result = string.Empty;

        try
        {
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://XXXXXXXX/rest/api/2/issue"))
                {
                    request.Headers.TryAddWithoutValidation("Accept", "application/json");

                    var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("myemail@email.com:wewewewewewwewe"));
                    request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

                    request.Content = new StringContent("{\"fields\" : {\"project\": {\"key\" : \"MDM\"},\"summary\" : \"REST ye merry gentlemen.\",\"description\" : \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\" : {\"name\": \"Bug\" } } }", Encoding.UTF8, "application/json");

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

        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }
1 Like

I am using restclient.
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes(“myemail@email.com:wewewewewewwewe”));
wewewewewewwewe → it’s your api token or password.