0

I want to get my projects from Jira. So I write this code but i get this error; The remote server returned an error: (401) Unauthorized.

my code is;

protected string RunQuery(JiraResource resource, string argument = null, string data = null, string method = "GET")
{
    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;

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

    if (data != null)
    {
        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;
}

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

do you have got any idea about this error ? how can i fix this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Your code works for me. Maybe you’re just trying to fetch data from a part of Jira that you don’t have access to? Maybe the API access is restricted? Check with a Jira administrator if you suspect this might be the case. – Rapunsel Jun 03 '16 at 07:08
  • i fixed the problem. the problem is completly my account and passwords. but i want to ask another question about it. if i want to access jira same acocunt and 2 different client is it problem ? why i saw this but i saw a problem today about it. when i try to 2 different client with my account to jira then i saw remote server error. when i checked the orginal login page in jira. it asked to me captcha. is it reason about it or not ? thank you – Ben Van der Ham Jun 03 '16 at 13:38

0 Answers0