无法获取身份验证令牌

时间:2020-03-03 05:47:54

标签: angular api postman angular-cli

我正在尝试使用以下代码获取身份验证令牌,但没有得到任何身份验证令牌。但是,我在邮递员那里得到了令牌。

fetchPeople() { 
    //return this.http.post('http://169.38.82.132:94/token',data,{ headers: this.headers }); 
    let url = "http://169.38.82.132:94/token";
    var headers = new HttpHeaders();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Accept', 'application/json');
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.set('grant_type', 'password');
urlSearchParams.set('username', 'user');
urlSearchParams.set('password', 'user');
//let body = urlSearchParams.toString();
const data = 'grant_type=password';
return this.http.post( url, data,
  {headers: new HttpHeaders({
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  })}  
  )
  };  
}
console.log(data);

我错过了任何逻辑设置吗?请指导我获取身份验证令牌。在控制台中

grant_type=password&username=user&password=user
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Live Reloading enabled.
POST http://169.38.82.132:94/token 400 (Bad Request)

1 个答案:

答案 0 :(得分:0)

fetchPeople() { 
    //return this.http.post('http://169.38.82.132:94/token',data,{ headers: this.headers }); 
    let url = "http://169.38.82.132:94/token";    
const data = 'grant_type=password&username=user&password=user';
return this.http.post( url, data,
  {headers: new HttpHeaders({
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  })}  
  ).subscribe(resp => {
    console.log(resp);
})
  };  
}
console.log(data);

http帖子返回一个可观察到的消息,因此您可以订阅并处理您的回复