如何将值分配给angular

时间:2019-06-05 17:26:44

标签: angular http get

我解释。我正在向具有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);
  }

}

1 个答案:

答案 0 :(得分:0)

尝试从结果中删除引号:

    this.httpOptions.set('Accept',result.token_type);
    this.httpOptions.set('Authorization',result.access_token);