简体   繁体   English

将图像上传到 imgur api 不断响应错误“:”需要身份验证

[英]Upload image to imgur api keeps responding error“:”Authentication required

I have been trying to upload an image with imgur api and react, but I keep getting the same error even though I am including my Client ID.我一直在尝试使用 imgur api 上传图像并做出反应,但即使我包含我的客户 ID,我仍然会收到相同的错误。

imgur docs: https://apidocs.imgur.com/?version=latest#c85c9dfc-7487-4de2-9ecd-66f727cf3139 imgur 文档: https://apidocs.imgur.com/?version=latest#c85c9dfc-7487-4de2-9ecd-66f727cf3139

the response from the server:来自服务器的响应:

{"data":{"error":"Authentication required","request":"\/3\/image","method":"POST"},"success":false,"status":401}

This is how I get the image:这就是我获取图像的方式:

 <input
            type="file"
            style={{ display: "none" }}
            ref={(fileInput) => (this.fileInput = fileInput)}
            onChange={this.uploadImg}
          />

Here is the onChange function:这是 onChange function:

uploadImg(e) {
    const data = new FormData();
    data.append("image", e.target.files[0]);
    const config = {
      headers: {
        "Content-type": "application/x-www-form-urlencoded",
        Authorization: "Client-ID xxx",
      },
    };
    axios.post("https://api.imgur.com/3/image", config, data);
  }

Am I doing something wrong here?我在这里做错了吗? I have been trying to do it for the past couple of hours and couldn't find anything to help me solve this issue在过去的几个小时里,我一直在尝试这样做,但找不到任何东西可以帮助我解决这个问题

I have managed to solve this issue.我已经设法解决了这个问题。 The problem was with my post request, apparently it takes data as the first argument and options as the second.问题出在我的发布请求上,显然它将数据作为第一个参数,将选项作为第二个参数。

Changing this line:更改此行:

axios.post("https://api.imgur.com/3/image", config, data);

With this line:有了这条线:

axios.post("https://api.imgur.com/3/image", data, config);

Seems to have solved the issue似乎已经解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM