【发布时间】:2019-06-05 17:26:44
【问题描述】:
我解释一下。我正在向一个带有一些参数的 api 发布请求发出请求,它返回一个令牌,我必须将其包含在我稍后执行的 get 请求中。到现在为止还挺好。问题是当我要获取请求时,在该请求的标头中,我必须包含我在 POST 请求中获得的 access_token 和 type_token,因为我声明了 Angular 中指定的 HttpHeaders 类型的 httpOptions 对象文档,但我不能让它工作。我究竟做错了什么?在这里我留下我到目前为止所做的代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Inmueble } from '../modelos/inmueble';
@Injectable({
providedIn: 'root'
})
export class HostlistService {
httpOptions = {
headers: new HttpHeaders({
'Accept': '',
'Authorization': ''
})
}
parametros = {
'grant_type':'client_credentials',
'client_id': 1,
'client_secret': 'clientSecret'
}
constructor(public http: HttpClient) {
}
obtenerToken(){
return this.http.post<any>('URL/oauth/token',this.parametros).subscribe(
result => {
this.httpOptions.set('Accept','result.token_type');
this.httpOptions.set('Authorization','result.access_token');
},error =>{
console.log(error);
}
);
}
obtenerInmuebles(){
return this.http.get('URL',this.httpOptions);
}
}
【问题讨论】: