REST API C# POST request auth error - The remote server returned an error: (401) Unauthorized.

HH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 1, 2019

Hopefully someone can help me as this is driving me insane!! 

Via the REST API I'm attempting to make a POST request to our Service Desk from a C# web form. 

I have tried to make the connection with my username and password. I am an administrator and so have access to the full system.

I have also tried it the following ways:

All of these return the same error: 

The remote server returned an error: (401) Unauthorized. 

This is my code: 

try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

WebRequest req = WebRequest.Create(@"https://myurl.co.uk/rest/servicedeskapi/request");
req.Method = "POST";
req.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(
Encoding.Default.GetBytes("username:password"));

req.ContentType = "application/json";

var json = JObject.Parse(
"{\"serviceDeskId\": \"4\",\"requestTypeId\": \"30\",\"requestFieldValues\": {\"summary\": \"" +
summary.Value + "\",\"description\": \"" + description.Value + "\"}}");

using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(json);
}

HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
}
catch (Exception ex)
{
error.InnerText = ex.Message;
}

Any help would be appreciated! 

1 answer

0 votes
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 3, 2019

Hi @HH ,

The way to authenticate REST API calls in Cloud is to use the email address associated to your  Atlassian Account and the API Token.

Now, in order to  check that you are using the right credentials and/or if your auth requests are failing since you need to complete the captcha, can you kindly run below command and paste the output masking the sensitive data:

curl -D- -u <email_address>:<api_token> -X GET https://<hostname>/rest/api/2/project

 

If everything goes fine the above call will return the list of all the projects the Atlassian Account you used to authenticate the REST API call has access to (plus, the -D- option will print the full response header).

 

Cheers,
Dario

Suggest an answer

Log in or Sign up to answer