Imgur api returns 仅在匿名上传图片时出现 401 状态
Imgur api returns 401 status only while uploading image anonymously
所以我注册了我的应用程序并获得了我的 client_id 和 client_secret。我设置了我的选项并尝试上传图像,但我一直收到 401 状态消息 "Authentication required"。使用 get 尝试我的代码并成功检索到图像。只有当我尝试 post 图像时才会发生错误”。一直在寻找答案,但每个人都在 header 中丢失了他们的 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;
}));
我不确定我还能做什么...我在 postman 中使用了我的 client_id,它在那里工作。
您正在使用 post 方法,该方法接受数据作为第二个参数,选项作为第三个参数。但是您已经传递了选项来代替数据参数。改为这样做
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));
}
此外,您可以使用 tap
运算符执行简单的 console.log
,这是 side-effects.
的目标
所以我注册了我的应用程序并获得了我的 client_id 和 client_secret。我设置了我的选项并尝试上传图像,但我一直收到 401 状态消息 "Authentication required"。使用 get 尝试我的代码并成功检索到图像。只有当我尝试 post 图像时才会发生错误”。一直在寻找答案,但每个人都在 header 中丢失了他们的 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;
}));
我不确定我还能做什么...我在 postman 中使用了我的 client_id,它在那里工作。
您正在使用 post 方法,该方法接受数据作为第二个参数,选项作为第三个参数。但是您已经传递了选项来代替数据参数。改为这样做
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));
}
此外,您可以使用 tap
运算符执行简单的 console.log
,这是 side-effects.