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



我正在处理一个要求,其中我需要通过使用REST API来一次创建多个问题。但是,我从上传单个问题开始,因为我是API集成的新问题。我在C#中写几行代码。这是我的代码:

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

但是,当我运行上述代码时,我会遇到" 400"错误。我谷歌搜索了它,发现当URL或用户名/密码不正确时,此错误通常会出现。我交叉检查了这两件事,但是正确。

我可以知道为什么这个错误即将到来,还是解决问题的解决方案是什么?

您的密码不是您的登录密码,它是您从这里获得的API令牌:

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

生成一个令牌,然后将其用作密码。

最新更新