简体   繁体   English

为什么使用REST API(C#)创建JIRA问题时出现错误?

[英]Why the error in coming while creating a JIRA issue using REST API (C#)?

I am working on a requirement where I need to create multiple issues in one go by Using the REST API. 我正在研究一项需要使用REST API一次性创建多个问题的需求。 However, I start with uploading a single issue because I am new in API integration. 但是,我从上传单个问题开始,因为我是API集成的新手。 I write few lines of code in c#. 我用C#编写了几行代码。 Here is my code: 这是我的代码:

static void Main(string[] args)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        JiraCreateIssueRequest jcir = new JiraCreateIssueRequest();
        //////////////////////////////////////////////////////////////////
        string sUsername = "aaa@xyz.com";
        string sPassword = "TestPassword";
        string uri = @"https://domain.atlassian.net/rest/api/2/issue";
        Uri address = new Uri(uri);
        HttpWebRequest request;
        //HttpWebResponse response = null;
        StreamReader sr;
        string sData = null;
        string returnXML = string.Empty;
        if (address == null) { throw new ArgumentNullException("address"); }
        //jcir.project.ID = 100;
        //jcir.Summary = "This issue is created by JIRA REST Api";
        //jcir.Description = "This issue is created by JIRA REST Api";
        //jcir.IssueType.ID = 1;
        sData = @"{""fields"":{""project"":{""key"": ""SITT""},""summary"": ""REST API Uploading"",""description"":""Creating an issue via REST API"",""issuetype"": {""name"": ""Test""}}}";
        //sData = jcir.ToString();
        try
        {
            // Create and initialize the web request
            request = WebRequest.Create(address) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json";
            // Add the Authorization header to the web request
            request.Credentials = new NetworkCredential(sUsername, sPassword);
            //Write Data
            if (sData != null)
            {
                byte[] byteData = UTF8Encoding.UTF8.GetBytes(sData);
                // Set the content length in the request headers
                request.ContentLength = byteData.Length;
                // Write data
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(byteData, 0, byteData.Length);
                }
                // Get response
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Get the response stream
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    string str = reader.ReadToEnd();
                }
            }
        }
        catch (WebException wex)
        {
            // This exception will be raised if the server didn't return 200 - OK
            // Try to retrieve more information about the error
            if (wex.Response != null)
            {
                using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
                {
                    try
                    {
                        string sError = string.Format("The server returned '{0}' with the status code {1} ({2:d}).",
                        errorResponse.StatusDescription, errorResponse.StatusCode,
                        errorResponse.StatusCode);
                        sr = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8);
                        returnXML = sr.ReadToEnd();
                    }
                    finally
                    {
                        if (errorResponse != null) errorResponse.Close();
                    }
                }
            }
            else
            {
                throw new Exception(wex.Message);
            }
        }
    }

    public class JiraCreateIssueRequest
    {
        protected JavaScriptSerializer jss = new JavaScriptSerializer();

        public JiraProject project = new JiraProject();
        public string Summary { get; set; }
        public string Description { get; set; }
        public JiraIssueType IssueType = new JiraIssueType();

        public override string ToString()
        {
            return jss.Serialize(this);
        }
    }

    public class JiraCreateIssueResponse
    {


    }

    public class JiraProject
    {
        public int ID { get; set; }
        //public string Key { get; set; }
    }

    public class JiraIssueType
    {
        public int ID { get; set; }
        //public string Name { get; set; }
    }

But when I am running the above code, I am getting the '400' error. 但是,当我运行上述代码时,出现了“ 400”错误。 I googled it and found that this usually this error comes when the URL or the Username/Password are incorrect. 我用Google搜索它,发现通常是当URL或用户名/密码不正确时出现此错误。 I cross checked both the things however its correct. 我交叉检查了这两件事,但它是正确的。

May I know why this error is coming or what will be the resolution of the problem? 我能否知道为什么会出现此错误,或者该问题的解决方案是什么?

Your password is not your login password, it's an API token that you get from here: 您的密码不是您的登录密码,而是您从此处获得的API令牌:

https://id.atlassian.com/manage/api-tokens https://id.atlassian.com/manage/api-tokens

Generate a token, then use that as your password. 生成令牌,然后将其用作密码。

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

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