我试图从401响应中检查HttpHeaders以查看auth令牌是否需要刷新但是错误中的HttpHeaders对象没有内部我做错了或者这是一个bug,因为我可以看到chrome控制台窗口中的标题:
get Header() {
let headers = new HttpHeaders();
headers = headers.set('Authorization', `Bearer ${this.authService.accessToken}`);
const options: any = { headers: headers, observe: 'response'};
return options;
}
Observable.interval(this.interval)
.switchMap(() => this.http.get(this.Url, this.Header).take(1))
.subscribe(
(data) => { console.log('got data'); },
(err: HttpErrorResponse) => {
console.log(JSON.stringify(err.headers));
}
);
这是来自错误的控制台日志:
{"normalizedNames":{},"lazyUpdate":null,"headers":{}}
这里是从chrome
中捕获的响应标头HTTP/1.1 401 Unauthorized
Server: Kestrel
WWW-Authenticate: Bearer error="invalid_token", error_description="The access token is no longer valid."
Access-Control-Allow-Origin: *
Request-Context: appId=cid-v1:70b5c92d-7561-44ce-ade9-dcf9c4bd6372
X-Correlation-Id: 0HLA7HBHN01NA:0000003E
X-Powered-By: ASP.NET
Date: Wed, 20 Dec 2017 10:43:13 GMT
Content-Length: 0
请注意,包含将其建议为重复的代码无法解决问题