使用 redmine API 进行 POST 请求时出现 401 错误,即使我包含了 api 键

Getting 401 error when using redmine API for a POST request even though I have included the api key

我正在尝试发出 post 请求,以使用 redmine-api 创建一个新的 wiki 页面。我正在使用 JavaScript 和 Axios。但是我收到 401 错误(UnAuthorize)。

我的目标是能够将 word 文档发送到我的 redmine 并创建一个 wiki 页面。

我正在使用提供的 Api 键,我确实在我的 redmine 设置中启用了其余 api 功能

我已将 api 键包含在 header 中,但它不起作用。

  var wordDocument = "./Redmine.docx"

   axios.post('<website url>/uploads.json', {
    headers: {
        'Content-Type': 'application/octet-stream',
        'Cache-Control': 'no-store',
        'key': '<api-key>'
    },
    data:wordDocument

    })
    .then(function (response) { 
       console.log("succeeed--->  "); 
       console.log    (response) 
     })
    .catch(function (error) {
        console.log("failed----->  ");
        console.log(error.response.headers)
        console.log(error.message)
        console.log("failed----->  ");
    })


我收到状态:“401 未经授权”,

尝试使用文档中提到的其他身份验证方法:

x passed in as a "key" parameter
- passed in as a username with a random password via HTTP Basic authentication
- passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0)

https://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication

还要确保您使用的是正确的 API 密钥。

You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout.

好的,我让它工作了。 我做了 "axios({})" 而不是 "axios.post"。不知道有什么不同?我以为是一样的。 这是我的代码,供 运行 参与其中的任何人使用。\

var wordDocument = "./Redmine.docx"
axios({
    method: 'post',
    url: '<redmind_url>/uploads.json',
    headers: { 'Content-Type': 'application/octet-stream'},
    params: { 'key': '<api key>'},
    data: wordDocument
})
    .then(function (response) {
        console.log("succeeed--->  ");
        console.log(response.data)
    })
.catch(function (error) {
    console.log("failed----->  ");
    console.log(error.response.statusText, "-->", error.response.status);
    console.log(error.response.headers)
    console.log(error.message)
    console.log("failed----->  ");
})