【发布时间】: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