401 (Unauthorized) from Atlassian Cloud API

I am working on doing some automation with JIRA, through my Atlassian Cloud (previously OnDemand) instance. My goal is to create a .NET application in C# that can interact with JIRA and automate things as well as provide specific bits of information.

I thought I was going crazy because I just could not get it to work. No matter what I did, or what I tried, I kept getting a 401 (Unauthorized) response when connecting to the API. And more specifically, this is what part of the response from the API looked like:

HTTP/1.1 401 Unauthorized
 X-Seraph-LoginReason: OUT
 X-Seraph-LoginReason: AUTHENTICATED_FAILED
 X-Seraph-LoginReason: AUTHENTICATED_FAILED
 Content-Length: 14172

I was using the library RestSharp and first suspected that it was an issue with using Basic Authorization, or that my base URL was incorrect. I also dug all through the JIRA control panel making sure API access was enabled and a host of other things. I routed my calls through Fiddler so I could inspect them. I searched Stack Overflow, JIRA documentation, and the whole of the internet! After spending at least an hour messing around I decided to head to bed. Now, 15 minutes into a new day, I figured out the issue.

The username is your actual JIRA username and NOT your email address.

The solution was simple: The username is your actual JIRA username and NOT your email address.

When using RestSharp, all of the communication is done via a class called RestClient. To configure this I am doing this:

var authenticator = new HttpBasicAuthenticator(credentials.UserName, credentials.Password);
restClient = new RestClient()
{
    BaseUrl = new Uri(configuration.BaseUrl),
    Authenticator = authenticator,
};

The credentials and configuration objects contain my JIRA username (not email address!), password, and base URL (“https://mysite.atlassian.net”).

From there nothing else too interesting happens, just standard RestSharp code. If you’re interested in seeing how more interactions can occur between RestSharp and the Atlassian API, check out the Jira.SDK project on GitHub which uses RestSharp.

Leave a Reply

Your email address will not be published. Required fields are marked *