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;
}
I've just noticed that your baseURL is using http instead of https - try changing it to https
Yes i tried with https. but still same issue . please suggest
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
do we need any references to add in console application. before executing that code
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 resourceWhat 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 :
Hopefully this will help you to get going.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
JiraResource is a enum property to call
public enum JiraResource
{
project,
myself
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is ProjectDescription that you use in your code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes,with /project. i am getting this error The remote server returned an error: (401) Unauthorized
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.