Forums

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

Unauthorized exception for HttpClient

Shubh Dasgupta October 9, 2018

I have this piece of code where I am trying to get details of a Jira ticket. The HttpResponseMessage always return Un-Authorized. My Jira is a SSO based authentication, so I don't have a username so I have tried with user-name@domain.com too, but no luck.

string url = String.Format("{0}rest/api/2/issue/{1}", "https://abc.atlassian.net/", "PROJ-001");
HttpClient client = new HttpClient();
client.BaseAddress = new System.Uri(url);
byte[] cred = Encoding.ASCII.GetBytes("user-name:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage messge = client.GetAsync(url, HttpCompletionOption.ResponseContentRead).Result;

if (messge.IsSuccessStatusCode)
{
    string result = messge.Content.ReadAsStringAsync().Result;
    details = result;
}

return details;

Language : C#

Status Code : System.Net.HttpStatusCode.Unauthorized

 

 

1 answer

1 accepted

1 vote
Answer accepted
Shubh Dasgupta October 9, 2018

Finally got it working. Answer by Sharoon helped : https://community.atlassian.com/t5/Jira-questions/JIRA-Rest-API-authentication-always-returns-401-unauthorized/qaq-p/187181

 

The username IS the full email.

The password is NOT your password to login, but an API token. To generate an API token, go to https://id.atlassian.com/manage/api-tokens# and create a new one.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events