繁体   English   中英

使用C#的JIRA SERVER中的Jira Rest API登录错误

[英]Jira Rest API login error in JIRA SERVER using C#

我想使用C#Rest API连接到Jira服务器

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;

远程服务器返回错误(404)

我尝试了不同的解决方法,但都徒劳无功。 我可以知道为什么会出现此错误吗? 解决该错误的方法可能是什么?

您可以通过多种方式搜索此错误的原因:

  • 通过查看JIRA服务器的日志,那里可能有一些消息/堆栈跟踪(例如atlasian-jira.log);
  • 通过使用某种工具执行/调试/测试REST调用(例如邮递员),当它开始在工具中工作时,您可以编写代码以编程方式进行操作。 JIRA可以在响应中返回错误说明,工具可以向您显示错误说明。

当您获得此信息时,它可以为您提供无法正常工作的确切原因。 一旦出现403错误,那是因为超出了失败登录尝试的阈值,我使用Web浏览器登录了JIRA服务器(并输入了验证码),此后,我能够通过应用程序代码获得会话。

我可以使用邮递员通过以下方式从JIRA成功获取会话:
请求类型:POST
网址: https//myjiraserver.com/rest/auth/1/session
{"username":"myusername","password":"mypassword"}{"username":"myusername","password":"mypassword"}
标头:Content-Type:application / json

您可以执行以下操作:

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;
    }
  }
 }

现在您可以像这样调用此类:

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

希望能帮助到你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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