0

Just started at a new company and we all use Jira, the customers are determined to not use it as they don't like it so I have decided to build a simple Windows Form when they can both Log tickets and get Updates and Comments in a nice simple UI.

Now I have never done any coding before 2 weeks ago so it has been a struggle to get my head around both C# and Rest (Have made scripts for basic IT fixes but never anything as complex as this!)

Back onto point, Set up and got a Rest API set up with a Rest Client but everytime I try pull data from a ticket on Jira I get the error:

{"errorMessages":["You do not have the permission to see the specified issue.","Login Required"],"errors":{}}

Here is the code from the Form:

private void button3_Click_1(object sender, EventArgs e) { var client = new RestClient("https://jira.eandl.co.uk/rest/api/2/issue/ITREQ-" + textBox1.Text ); client.Authenticator = new SimpleAuthenticator("username", "abc", "password", "123"); var request = new RestRequest(Method.GET); request.AddParameter("token", "saga001", ParameterType.UrlSegment); // request.AddUrlSegment("token", "saga001"); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var queryResult = client.Execute(request); Console.WriteLine(queryResult.Content); } 

And here is the code from the Rest Client itself:

public Restclient() { endPoint = string.Empty; httpMethod = httpVerb.GET; } private string logonAttempt; public string makeRequest() { string strResponseValue = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint); request.Method = httpMethod.ToString(); String authHeader = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(userName + ":" + userPassword)); request.Headers.Add("Authorization", authType.ToString() + " " + authHeader); HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); //Process the Response Stream... (Could be JSON, XML ot HTML etc...) using (Stream responseStream = response.GetResponseStream()) { if (responseStream != null) { using (StreamReader reader = new StreamReader(responseStream)) { strResponseValue = reader.ReadToEnd(); }//End of Stream Reader } }//end of Response Stream } catch (Exception ex) { strResponseValue = "(\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}"; } finally { if(response != null) { ((IDisposable)response).Dispose(); } } return strResponseValue; } } } 

Now obviously I am expecting that I have missed something absolutely bigginer as like I said, I've never taken on a project like this before and had 0 experience.

Just looking for someone to bluntly tell me what I'm doing wrong

Changed to this as per answer:

private void button3_Click_1(object sender, EventArgs e) { var client = new RestClient("https://jira.eandl.co.uk/rest/api/2/issue/ITREQ-" + textBox1.Text ); client.Authenticator = new HttpBasicAuthenticator("username", "password"); var request = new RestRequest(Method.GET); string authHeader = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("cdale!" + ":" + "Chantelle95!")); request.AddHeader("Authorization", "Basic " + authHeader); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var queryResult = client.Execute(request); Console.WriteLine(queryResult.Content); } 

2 Answers 2

1

By default with the Jira REST API, you can use Basic Authentication or OAuth2. I think that more easy way for you will be to use the Basic one.

I'm not sure why you have a class where you define your custom RestClient since the first block of code uses the RestSharp one from http://restsharp.org.

In this case, you will need to modify your authenticator:

client.Authenticator = new HttpBasicAuthenticator(userName, password); 

And I think that you should remove the line where you specify a token. I don't think that it's required.

Finally, the class Restclient doesn't seem to be used, then remove it.

You could also uses what you have created in your custom RestClient and manually specify a Basic header:

string authHeader = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(userName + ":" + userPassword)); request.AddHeader("Authorization", "Basic " + authHeader); 

However, it's essentially the behavior of the HttpBasicAuthenticator class.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the advice! Given it a try and getting the same error though. The RestClient Class is a bit of forgetting to do house work, Originally set up to use it then a friend told me about RestSharp being useful for APIs so thought id try it!
@ConnorDale You should use one way to add your Authorization HTTP header (HttpBasicAuthenticator vs AddHeader), but not both. If you open a browser and go to the following URL jira.eandl.co.uk/rest/api/2/issue/ITREQ-XXXX (where XXXX is the ID of your issue) and you enter credentials when asked, do yo got the expected response or the same error?
If I go to the URL then I get all the json data for whatever issue I was looking as I would expect. It's only through the App that I get the error RE Permissions
0

If you don't want to encode your credentials in every request here is how to do it using cookies.

When requesting the cookie you don't need to add any authorization on the headers. This method will accept a JSON string with the user name and password and the URL. It will return the cookie values.

public async Task<JiraCookie> GetCookieAsync(string myJsonUserNamePassword, string JiraCookieEndpointUrl) { using (var client = new HttpClient()) { var response = await client.PostAsync( JiraCookieEndpointUrl, new StringContent(myJsonUserNamePassword, Encoding.UTF8, "application/json")); var json = response.Content.ReadAsStringAsync().Result; var jiraCookie= JsonConvert.DeserializeObject<JiraCookie>(json); return jArr; } } public class JiraCookie { public Session session { get; set; } } public class Session { public string name { get; set; } public string value { get; set; } } 

When I call it using url: http://[baseJiraUrl]/rest/auth/1/session it returns the following JSON response:

{ "session" : -{ "name" : JSESSIONID, "value" : cookieValue } 

Keep in mind the URL above is valid in the version of JIRA I'm using and may vary depending on which version you're using. Read the JIRA API documentation for the correct URL for the version you are using. I'm using the following: https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#auth/1/session

Remember you'll have to store your cookie and use it on every subsequent request. Check out this answer on how add cookies to your HttpClient request: How do I set a cookie on HttpClient's HttpRequestMessage.

Once you're done with the cookie (logging out) simply send a delete http request with the same URL as the post.

Reference: https://stackoverflow.com/a/49109192/7763903

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.