Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Unauthorized exception for HttpClient

Shubh Dasgupta October 9, 2018 edited

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
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Events