简体   繁体   中英

Jira Rest API login error in JIRA SERVER using C#

I want connect to jira server using C# Rest api

https://jira.myserver.co.kr/rest/auth/1/session

enter code here

 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 request.ContentType = "application/json";
 request.Method = method;
 ... more
  HttpWebResponse response = request.GetResponse() as HttpWebResponse;

the remote server returned an error (404)

I tried different work arounds but all went in vain. May I know why this error is coming? What could be the resolution of this error?

You can search for a reason of this error in different ways:

  • by looking at logs of JIRA server, there might be some messages/stacktraces there (for example, atlasian-jira.log);
  • by using some tool to perform/debug/test REST calls (for example, postman), and when it's start working in tool you can write code to do it programmatically. JIRA can return description of error in the response, and tool can show it to you.

When you get this information it can give you exact reason why it is not working. Once I got 403 error and it was because threshold of unsuccessful login attempts was exceeded, I logged into JIRA server using web browser (and entered captcha), and after that I was able to obtain session through application code.

I can successfully obtain session from JIRA in the following way using postman:
Request type: POST
URL: https://myjiraserver.com/rest/auth/1/session
Body: {"username":"myusername","password":"mypassword"}
Headers: Content-Type:application/json

you can do something like this:

namespace YOUR_NAME_SPACE
{
  public class jira
  {
    public static string createTicket(string url, string data)
    {
        try
        {
            var client = new System.Net.Http.HttpClient();
            string base64Credentials = GetEncodedCredentials();
            var header = new AuthenticationHeaderValue("Basic", base64Credentials);
            client.DefaultRequestHeaders.Authorization = header;

            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var result = client.PostAsync(url, content).Result;
            var response = result.Content.ReadAsStringAsync().Result;
            // You can call putIssue if you want
            return response;
        }
        catch (System.Net.WebException ex)
        {
            Console.WriteLine("Exception Occurred" + " : {0}", ex.Message);
            throw;
        }
    }

    private static string GetEncodedCredentials()
    {
        string mergedCredentials = string.Format("{0}:{1}", "LOGIN", "PASSWD");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        return Convert.ToBase64String(byteCredentials);
    }

    public static string jiraSerialise(string project, string summary, string description, string issutype, string author)
    {
        JObject valuesToJson =
           new JObject(
               new JProperty("fields",
                   new JObject(
                       new JProperty("project",
                            new JObject(new JProperty("key", project))),
                       new JProperty("summary", summary),
                       new JProperty("description", description),
                       new JProperty("issuetype",
                            new JObject(new JProperty("name", issutype))),
                       new JProperty("assignee",
                            new JObject(new JProperty("name", author))))));
        return valuesToJson.ToString();
    }

    public static string putSerialize(string key, string value)
    {
        JObject valueToJson =
            new JObject(
                new JProperty(key, value));
        return valueToJson.ToString();
    }

    public static string putIssue(string response, string author, System.Net.Http.HttpClient client)
    {
        JObject jsonResponse = JObject.Parse(response);
        Dictionary<string, string> dictResponse = jsonResponse.ToObject<Dictionary<string, string>>();
        string issueUrl = dictResponse.Last().Value;
        string issueAssignee = issueUrl + "/assignee";
        var authorContent = new StringContent(author, Encoding.UTF8, "application/json");
        var authorResult = client.PutAsync(issueAssignee, authorContent).Result;
        var authorResponse = authorResult.Content.ReadAsStringAsync().Result;
        Console.WriteLine(authorResponse);
        return authorResponse;
    }
  }
 }

And now you can call this class like that:

 string data = jira.jiraSerialise("lala", "nameVulnerabilty", "descriptionField", "Bug", "author");
 string url = "http://YOUR_URL/rest/api/2/issue/";                                           
 Console.WriteLine(jira.createTicket(url, data));

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM