0

我有一个 API,我需要使用它来创建报告,但是该 API 需要用户名和密码才能访问它,通过使用 curl 命令我可以访问 API

curl -X GET "https://SomeApiCall/api/v1/customers/" -H "accept:application/json; charset=UTF-8" -u "username:password"

但是当我尝试使用 angular 使用相同的 API 时,我遇到了错误

key:"api.error.invalid_auth_schema"
message: "Invalid authentication scheme is used. Only Basic Authentication scheme is supported (login and password should be provided)"

角度示例代码:

 this.data = this.http.get("https://SomeApiCall/api/v1/customers/" , {
      headers: new HttpHeaders({
        'Authorization': 'Basic' + btoa('username:password'),
        'Content-Type':  'application/json'
      })
    });

有人可以解释一下,我犯了什么错误吗?

4

1 回答 1

1

我认为问题在于您在“基本”之后没有空格。改成:

 this.data = this.http.get("https://SomeApiCall/api/v1/customers/" , {
  headers: new HttpHeaders({
    'Authorization': 'Basic ' + btoa('username:password'),
    'Content-Type':  'application/json'
  })
});
于 2021-01-25T08:30:33.670 回答