缺少Angular6 OAuth发布请求授予类型

时间:2018-10-31 05:58:01

标签: angular

授予类型应该位于标头中还是位于正文中?

当我在Postman上测试应用程序时,我成功获取了令牌。但是,尝试使用angular来实现始终获取Http状态代码:400缺少授权类型

{error: "invalid_request", error_description: "Missing grant type"}

这是我的代码

LoginComponent

credentials = {username: 'hi', password: '123', grant_type: 'password'};

AppServices

oAuthAuthenticated(credentials){

const headers = new HttpHeaders(
    {
      'Authorization' : 'Basic '+btoa(AppService.CLIENT_ID + ':' + AppService.SECRET),
      'Content-type' : 'application/x-www-form-urlencoded; charset=utf-8'
    }
  )

this.httpClient.post("/oauth/token", credentials, {headers})
    .pipe(
      catchError(this.handleError.bind(this))
    )
    .subscribe(response => {
      console.log(response);
    });

}

我确实尝试了几种组合,但我读到它不是使用URLSearchParams,而是使用HttpParams,因为它是不可变的并且是更好的做法。

let params = new HttpParams();
params = params.append('username', credentials.username);
params = params.append('password', credentials.password);
params = params.append('grant_type', credentials.grant_type);

如果您按照某些gitHub的建议执行此操作,并且根据此tutorial

this.httpClient.post("/oauth/token", params.toString(), {headers}) . -- status 400 missing grant type

如果我自己构造身体,它就会起作用。

let body ="grant_type=password&username="+credentials.username+"&password="+credentials.password+"&client_id="+AppService.CLIENT_ID;

//this.httpClient.post("/oauth/token", body,{headers}) -- okey

但是有更好的方法吗?并且希望有人能详细说明问题可能出在哪里,然后再构建自己的字符串以提交到服务器。也许是更好的方法。

0 个答案:

没有答案
相关问题