简体   繁体   English

创建问题时,Jira REST API返回错误

[英]Jira REST API returns error when creating issue

My goal is to use the Jira REST API to create an issue. 我的目标是使用Jira REST API来创建问题。 But as of now I am getting an error (405) Method Not Allowed. 但是截至目前,我收到一个错误(405)不允许的方法。

I have checked in the Jira Properties if the Jira Remote APIs are switched on. 我已经在“ Jira属性”中检查了是否已打开“ Jira远程API”。 Under: 下:

JIRA Configuration > General Configuration > Allow Remote API Calls is ON. JIRA配置>常规配置>允许远程API调用处于启用状态。

I have also checked my JSON string submitted which looks fine to me: 我还检查了提交的JSON字符串,它对我来说看起来不错:

"{\"fields\":{\"project\":{\"key\":\"CTTS\"},\"summary\":\"Api  Test\",\"description\":\"Test\",\"issuetype\":{\"name\":\"Story\"}}}"

My current code to call the API is following: 我当前调用该API的代码如下:

public JiraApiResponseObject CreateRequest(JSONstring)
    {
        jiraRequest = (HttpWebRequest)WebRequest.Create("https://MyJiraUrl.net/rest/api/2/issue");

        jiraRequest.Method = "POST";
        jiraRequest.ContentType = "application/json";
        jiraRequest.Accept = "application/json";

        using (var streamWriter = new StreamWriter(jiraRequest.GetRequestStream()))
        {
            streamWriter.Write(JSONstring);
            streamWriter.Flush();
        }

        HttpWebResponse response = (HttpWebResponse)jiraRequest.GetResponse();
        return jiraReturnObject;
    }

I currently dont know what is causing the error. 我目前不知道是什么原因引起的错误。 I just get the (405) Method Not Allowed error from the remote server, when i actually would expect a API response. 当我实际上期望API响应时,我只是从远程服务器收到(405)Method Not Allowed错误。

There Is an Authorization Header Missing in the WebRequest. WebRequest中缺少授权标头。 Jira needs such a header to confirm that only authorized users can access the API's. Jira需要这样的标头,以确认只有授权用户才能访问API。

This Authorization String is is build as follows: 此授权字符串是如下构建的:

"Basic username:api_token"

The api_token can be generated in the Jira Cloud and needs to be base64-encoded. api_token可以在Jira Cloud中生成,并且需要进行base64编码。

Here is what I would do: 这是我会做的:

  1. Generate an API token for Jira using your Atlassian Account: https://id.atlassian.com/manage/api-tokens . 使用您的Atlassian帐户为Jira生成API令牌: https : //id.atlassian.com/manage/api-tokens
  2. Build a string of the form username:api_token. 构建一个格式为username:api_token的字符串。
  3. Base64-encode encode the string. Base64编码对字符串进行编码。
  4. Supply an Authorization header with content Basic followed by the encoded string. 提供带有内容Basic的Authorization标头,后跟编码的字符串。

For example, the string fred:fred encodes to ZnJlZDpmcmVk in base64, so you would add the following to your request: 例如,字符串fred:fred在base64中编码为ZnJlZDpmcmVk,因此您可以在请求中添加以下内容:

jiraRequest.Headers["Authorization"] = "Basic " + "UserName" + Base64Encode(apiToken);

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

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