Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to login with rest api c#

Naveenachary Maroju
November 29, 2018

When i try to login with rest api using c# . i am not get any response after request is submitted

below is c# sample code i am using to get all projects which i got from the community suggestions. please suggest

public class JiraManager
{
private const string m_BaseUrl = "http://companyname.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;
}

 

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

string projectsString = RunQuery1(JiraResource.project);

return JsonConvert.DeserializeObject<List<ProjectDescription>>(projectsString);
}

protected string RunQuery1(
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;

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

3 answers

0 votes
Warren
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 Champions.
November 29, 2018

I've just noticed that your baseURL is using http instead of https - try changing it to https

Naveenachary Maroju
November 29, 2018

Yes i tried with https. but still same issue . please suggest

Naveenachary Maroju
November 29, 2018

do we need any references to add in console application. before executing that code

Warren
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 Champions.
November 29, 2018

I would keep using https, because I think it's required.

I've noticed that this line

string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());

has an extra / after {1} which shouldn't be there

Also, the first parameter of RunQuery1 is

JiraResource resource

What is JiraResource? I changed it back to string

I created a new project, copied your code in, made the 2 changes mentioned above plus put my companyname in m_BaseURL. I then replaced your GetProjects function with a simple GetIssue which passes in issue/ABC-123 as listed above and it works. 

Please confirm that you've :

  1. Changed http to https
  2. Removed the extra /
  3. Changed JiraResource to string
  4. Changed companyname to your actual url
  5. Tried with a simpler call than GetProject

Hopefully this will help you to get going.

Naveenachary Maroju
November 29, 2018

tried with all corrections you said. but still i am having same issue . do i have to check any other settings . please suggest. if you have any sample working code i am trying in c# console application please let me know 

Warren
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 Champions.
November 29, 2018

Do you want to post all your code here again, so I can try it? You didn't answer what JiraResource is in your code

Naveenachary Maroju
November 30, 2018

JiraResource  is a enum property to call 

public enum JiraResource
{
project,
myself
}

0 votes
Naveenachary Maroju
November 29, 2018

Hi Warren,

I tried with multiple methods but i am facing same issue i.e The remote server returned an error: (401) Unauthorized. so do i want to do any config setting in my console application. please suggest or if u have sample code that helps me a lot 

0 votes
Warren
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 Champions.
November 29, 2018

Hi Naveenachary

I recognise the code you're showing as mine :-) and know that it has been working for 2 years and is still working, so we need to understand what is going wrong. Is it throwing an error?

What is response showing for your call? Possibly add a try catch block with the code below so that you can see what the response is if there is an exception.

 catch (WebException we)
{
Stream stream = we.Response.GetResponseStream();
}

The other thing to try is a simpler API call initially, maybe

RunQuery1("issue/ABC-123")

where you replace ABC-123 with a valid ticket key from your system. This simply returns all the fields for one ticket.

Warren
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 Champions.
November 29, 2018

What is ProjectDescription that you use in your code?

Naveenachary Maroju
November 29, 2018

Yes,with  /project.  i am getting this error  The remote server returned an error: (401) Unauthorized

Suggest an answer

Log in or Sign up to answer