来自Javascript的Finicity API握手失败并显示401,但可与Postman一起使用

时间:2018-06-22 16:31:02

标签: angular rest http cors postman

我使用的是Finicity API,在我的初步测试中,该软件在Postman上运行良好,但在Angular 5应用程序中使用时,在API调用的相关代码下方,HTTP状态为401失败。

this.token = '';
const authBody =
`<credentials><partnerId>${this.partnerId}</partnerId><partnerSecret>${this.partnerSecret}</partnerSecret></credentials>`;
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/xml').set('Accept', 'application/json').
  set('Finicity-App-Key', this.appKey);

const result = <Observable<boolean>>this.http.request('post', this.baseUrl + this.pathAuthentication,
   {body: authBody, headers: headers, observe: 'body'}).pipe(share());
result.subscribe( (data) => {
  if (data) {
    try {
      this.token = (<any>data).access.token;
      return of(true);
    } catch (e) {
      this.handleError(e);
      return of(false);
    }
  }
}, (error) => {
  this.handleError(error);
  return of(false);
});

该API https://api.finicity.com/aggregation/v2/partners/authentication应该返回用于整个会话的令牌,并且在Postman中可以正常使用。参见文档https://community.finicity.com/s/article/201704129-Step-1-Partner-Authentication

注意:我还设置了一个虚拟Web服务器,将整个请求转储为text和hex,并且Postman和Angular客户端都发送相同的请求。 此外,在为Angular应用程序提供服务的Web服务器中,还正确配置了CORS。

1 个答案:

答案 0 :(得分:0)

事实证明,Finicity后端无法正确响应飞行前请求 (下图显示了Chrome中的错误):

Chrome error in preflight request

另一方面,该请求在Postman和C#客户端上都可以正常工作(它们显然都不发出预检请求)。