【问题标题】:React + Atlassian Jira Cloud Api + Cors Anywhere: Adding a worklog via POST returns 403React + Atlassian Jira Cloud Api + Cors Anywhere:通过 POST 添加工作日志返回 403
【发布时间】:2019-03-10 11:55:45
【问题描述】:

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

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

这是我在 React 应用程序的 ComponentDidMount() 中进行的 fetch 调用。 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
        })

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

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

【问题讨论】:

    标签: reactjs jira-rest-api cors-anywhere


    【解决方案1】:

    您应该考虑构建an atlassian connect add-on,而不是使用基本身份验证。您可以使用 atlassian-connect-express 开始,它将处理插件的安装和 JWT 令牌的验证。基本上,您将能够从您的服务器进行安全调用(使用 JWT 令牌签名)或从您的前端(嵌入在 Jira 中)调用 Jira API,这将作为特定用户执行,这将生成适当的“更新由”条目。

    【讨论】:

    • 有趣!即使您拥有 Atlassian 托管的 Jira Cloud 实例(即 Jira 未托管在我拥有的服务器上),您能否构建 Atlassian Connect 插件?
    • 是的,当然。实际上,您只能为 Jira Cloud 构建 atlassian 连接插件。 Atlassian Connect 框架仅适用于 atlassian 云产品。
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2013-08-18
    • 2019-05-17
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多