React + Atlassian Jira Cloud Api + Cors Anywhere:通过 POST 添加工作日志 returns 403

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

这几天一直卡在这个问题上。我正在使用 Atlassian 的 Jira Cloud Rest API 在 React 中制作一个应用程序,我想从我的应用程序中记录 Jira 问题的工作。但是每次我尝试都会得到 403。Postman 中的相同请求有效,它创建了工作日志,这很奇怪。我设置了一个证书并使用 cors-anywhere 以便能够在我的 React 应用程序中使用 HTTPS,但查询是相同的。

我在 Atlassian 的论坛上阅读了很多关于人们在需要 POST 到 Atlassian API 时不确定该做什么的条目。这是无法修复的奇怪问题,还是我缺少相关的 header?

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

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":"user.name@gmail.com",
                      "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
        })

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

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

您应该考虑构建 an atlassian connect add-on. You could use atlassian-connect-express 而不是使用基本身份验证,它将处理附加组件的安装和 JWT 令牌的验证。基本上,您将能够从您的服务器进行安全调用(使用 JWT 令牌签名)或从您的前端(嵌入在 Jira 中)对 Jira API 进行调用,这些调用将作为特定用户执行,这将生成正确的 "updated by" 个条目。