Imgur API仅在匿名上传图像时返回401状态

时间:2019-06-06 17:01:15

标签: angular typescript httpclient imgur

所以我注册了我的应用并获得了client_id和client_secret。我设置了我的选项并尝试上传图像,但是我一直显示401状态,并显示消息“需要身份验证”。使用get尝试了我的代码并成功检索了图像。错误仅在我尝试发布图像时发生。”一直在寻找答案,但每个人都在标题中缺少他们的client_id,如您所见-我得到了我的帮助。

uploadImage(formData): Observable<object> {
const options = {
  headers: new HttpHeaders({
    Authorization: 'Client-ID <My_Client_ID>' // string here, replaced it for this question
  }),
  'mimeType': 'multipart/form-data',
  'contentType': false,
  'data': formData
};
return this.httpClient.post('https://api.imgur.com/3/image', options).pipe(map((res) => {
  console.log(res);
  return res;
}));

我不确定我还能做什么...我在邮递员中使用了client_id,它在那里工作。

1 个答案:

答案 0 :(得分:0)

您正在使用post方法,该方法接受数据作为第二个参数,并接受选项作为第三个参数。但是您已经传递了一些选项来代替data参数。代替

uploadImage(formData): Observable<object> {
  const options = {
    headers: new HttpHeaders({
      Authorization: 'Client-ID <My_Client_ID>'
    }),
   'mimeType': 'multipart/form-data',
   'contentType': false,
  };
  return this.httpClient.post('https://api.imgur.com/3/image', formData, options)
  .pipe(tap(console.log));
}

此外,您可以使用console.log运算符来执行简单的tap,这是产生副作用的方法。