update worklog C# "The remote server returned an error: (401) Unauthorized."

Irantha Jayasekara January 27, 2014

Im going to update worklog of an issue;

HttpWebResponse return this error "The remote server returned an error: (401) Unauthorized.".
Can anyone help me please?

Here is my code:

protected string RunQuery(JiraResource resource, string issue_id, string data = null, string method = "PUT")
{
//resource = issue
  // data :{"worklog":{"add":{"comment":"Sample test comment by IJ","started":"2014-01-28T19:25:33.78125+05:30","timeSpent":210}}}
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());
if (issue_id != null)
            {
                url = string.Format("{0}{1}", url, issue_id);
            }
   // url : https://company.atlassian.net/rest/api/2/issue/JIRA-16
            
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ContentType = "application/json";
            request.Method = method;
            request.ContentLength = data.Length;
 
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(data);
            }
 
            string base64Credentials = GetEncodedCredentials();
            request.Headers.Add("Authorization", "Basic " + base64Credentials);
 
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 
            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
 
            return result;
 
        }

where;

private string GetEncodedCredentials()
        {
            string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
            byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
            return Convert.ToBase64String(byteCredentials);
        }

2 answers

1 accepted

0 votes
Answer accepted
Irantha Jayasekara January 29, 2014

I should have add authorization header before sending request stream.
like below;

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.ContentType = "application/json";
        request.Method = method;
        request.ContentLength = data.Length;

        string base64Credentials = GetEncodedCredentials();
        request.Headers.Add("Authorization", "Basic " + base64Credentials);

        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write(data);
        }
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Karthick.S June 17, 2019

Hi , I tried the same , but it throwing the same error 

 

public class JiraManager
{
private const string m_BaseUrl = "https://emishealthgroup.atlassian.net/rest/api/2/";
private string m_Username;
private string m_Password;

public JiraManager(string username, string password)
{
m_Username = username;
m_Password = password;
}
public enum JiraResource
{
project,
myself
}

private string GetEncodedCredentials()
{
string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
//byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
byte[] byteCredentials = ASCIIEncoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
public string GetProjects()
{
//List<ProjectDescription> projects = new List<ProjectDescription>();

string projectsString = RunQuery1(JiraResource.project);
return projectsString;

//return JsonConvert.DeserializeObject<projectsString>;
}

protected string RunQuery1(JiraResource resource,string argument = null,string data = null,string method = "GET")
{
try
{
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());

if (argument != null)
{
url = string.Format("{0}{1}/", url, argument);
}

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
//request.ContentLength = data.Length;
string base64Credentials = GetEncodedCredentials();
request.Headers.Add("Authorization", "Basic " + base64Credentials);

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}

 

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}

return result;


}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw ex;
}

}

}

0 votes
Amy Chung October 13, 2017

 

Suggest an answer

Log in or Sign up to answer