关于reactjs:在任何地方反应Atlassian Jira Cloud Api Cors:通过POST添加工作日志将返回403

React + Atlassian Jira Cloud Api + Cors Anywhere: Adding a worklog via POST returns 403

现在,这件事持续了几天。我正在React中制作一个使用Atlassian的Jira Cloud Rest API的应用程序,我想从我的应用程序记录有关Jira问题的工作。但是,每次我尝试都得到403。Postman中的相同请求有效,它创建了工作日志,这很奇怪。我设置了一个证书,并在任何地方使用cors-where能够在我的React应用程序中使用HTTPS,但是在其他方面查询是相同的。

我已经在Atlassian的论坛上阅读了很多条目,这些条目使人们不确定何时需要发布到Atlassian API时该怎么做。这是无法解决的奇怪问题,还是我缺少相关的标头?

这是我在React应用程序的ComponentDidMount()中进行的访存调用。 https:// localhost:8080是我用于CORS-anywhere / Yarn的代理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fetch("https://localhost:8080/my-domain.atlassian.net/rest/api/2/issue/ISSUE-1/worklog,{
            headers: {
             "Access-Control-Allow-Origin":"https://localhost:3000/",
             "X-Atlassian-Token":"no-check",
             "Content-Type":"application/json;charset=UTF-8",
             "Authorization":"Basic base64token",

            },
            method:"POST",
            responseType: 'json',
            body: {
                "timeSpent":"2h 48m",
                "author":{
                     "accountId":"123456789",      
                     "active":true,
                     "avatarUrls":{
                            "16x16":"https://avatar-cdn.atlassian.com/...",
                            "24x24":"https://avatar-cdn.atlassian.com/...",
                            "32x32":"https://avatar-cdn.atlassian.com/...",
                            "48x48":"https://avatar-cdn.atlassian.com/..."
                      },
                     "displayName":"User Name",
                     "emailAddress":"[email protected]",
                     "key":"user.name",
                     "name":"user.name",
                     "self":"https://my-domain.atlassian.net/rest/api/2/user?username=user.name",
                     "timeZone":"Europe/Stockholm"
                  },
                 "comment":"bla bla bla",
                 "started":"2018-07-19T21:32:18.843+0200"
             }
        })
        .then((res) => res.json())
        .then(function(resJson){
            console.log(resJson
        })

这是我的Yarn运行的server.js。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const path = require('path')
const fs = require('fs')
const express = require('express')
const https = require('https')

const app = express();
const host = process.env.HOST || '0.0.0.0';
const port = process.env.PORT || 8080;

const cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
    httpsOptions: {
        key: fs.readFileSync(path.resolve('server.key')),
        cert: fs.readFileSync(path.resolve('server.crt'))
    },
    originWhitelist: ['https://localhost:3000', 'https://localhost:8080'],
    requireHeader: ['origin', 'x-requested-with'],
    removeHeaders: ['cookie', 'cookie2']
}).listen(port, host, function() {
    console.log('Running CORS Anywhere on ' + host + ':' + port);
});

您应该考虑构建Atlassian Connect附加组件,而不是使用基本身份验证。您可以首先使用atlassian-connect-express,它将处理附加组件的安装和JWT令牌的验证。基本上,您将能够从服务器进行安全调用(使用JWT令牌签名),或从将作为特定用户执行的前端(嵌入在Jira中)对Jira API进行调用,这将生成正确的"已更新"通过"条目。