angular - https://api.imgur.com/3/image 的 Http 失败响应
问题描述
我正在尝试将 base 64 编码图像上传到 imgur,但它一直失败并显示错误消息Http failure response for https://api.imgur.com/3/image: 403 OK
。我该如何解决?
@Injectable()
export class ImgurService {
private readonly IMGUR_UPLOAD_URL = 'https://api.imgur.com/3/image';
private readonly IMGUR_API_KEY = '<api-key-xxxx>';
constructor(
private logger: NGXLogger,
private http: HttpClient
) {
}
upload(b64Image: any) {
this.logger.debug('Handling file input');
this.logger.debug(image);
this.logger.debug(`Uploading picture to ${this.IMGUR_UPLOAD_URL}`);
const httpOptions = {
headers: new HttpHeaders ({
'Authorization': `Bearer ${this.IMGUR_API_KEY}`,
}),
};
const formData = new FormData();
formData.append('image', b64Image);
formData.append('album', 'profile');
return this.http.post<ImgurResponse>(`${this.IMGUR_UPLOAD_URL}`, formData, httpOptions);
}
}
回复:
error:
data: {error: "The access token provided is invalid.", request: "/3/image", method: "POST"}
status: 403
success: false
__proto__: Object
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure response for https://api.imgur.com/3/image: 403 OK"
name: "HttpErrorResponse"
ok: false
status: 403
statusText: "OK"
url: "https://api.imgur.com/3/image"
__proto__: HttpResponseBase
解决方案
您没有正确设置标题:您需要使用 set 或 append 因为对象是不可变的。
let headers = new HttpHeaders();
headers = headers.set('Authorization', 'Bearer ${this.IMGUR_API_KEY}');
您很可能会在浏览器中看到您的身份验证标头未发送。
推荐阅读
- amazon-web-services - 如何配置使用复合属性为我的 dynamodb 表创建分区键
- python - 发送对象数组时出错。OpenAPI、连接、烧瓶
- xaml - 如何根据 xaml 中的窗口大小动态更改文本和图像的位置
- java - ITextpdf 不适用于来自 html 的波兰语/捷克语字母
- solr - 当我们有两种不同类型的字段时,分数问题
- reactjs - 如何使用 Jest 和 React 测试库测试 React 的错误边界
- java - 如何在 Apache HttpClient 5.1 中跟踪 POST 的重定向
- python - 将具有不同数量的逗号分隔符的字符串转换为 pandas 中的列
- python - 附加具有不同第一个有效索引的列
- go - 返回具有测试期望的结构图 Go