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); }
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;
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;
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.