错误:“invalid_grant”Angular 5

时间:2017-12-12 10:15:17

标签: angular drupal

我正在尝试使用前面的Angular从Drupal接收访问令牌。

ngOnInit() {

  this.testAuthSite()
   .subscribe(
     data => { console.log(data); },
     error => { console.log(error); }
   );
}

// Multiple - Returns the list of all companies available for this user
testAuthSite(  ) {
  const body = {
    grant_type: 'password',
    client_id : '61705f72-xxx-xxx-xx',
    client_secret: 'admin',
    username: 'angular',
    password: 'admin',
    scope: null
  };
  const options: any = {
  headers: new HttpHeaders().set( 'Content-Type', 'application/x-www-form-urlencoded')
};

  return this.http.post(ApiURI, body, options);

}

我和邮递员一起尝试过它。

enter image description here

我为""密钥尝试了nullscope。两者都不起作用。

我收到了这个错误:

error:
  error:"invalid_grant"
  hint:"Check the configuration to see if the grant is enabled."
  message:"The provided authorization grant (e.g., authorization code, 
  resource owner credentials) or refresh token is invalid, expired, revoked, 
  does not match the redirection URI used in the authorization request, or 
  was issued to another client."
  __proto__:Object
  headers:HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
  message:"Http failure response for http://ecd2-
  oes.dev.ee.citeoeo.com/oauth/token: 400 Bad Request"
  name:"HttpErrorResponse"
  ok:false
  status:400
  statusText:"Bad Request"
  url:"http://xxxx.com/oauth/token"

以下是我发送内容的详细信息:

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。

POST请求不是Json而是FormData。所以我不得不将我的Json转换为FormData,如下所示:

testAuthSite(  ) {
  const body = {
   'grant_type': 'password',
   'client_id' : '61705f72-c913-43bf-b915-d679e82e8d58',
   'client_secret': 'admin',
   'username': 'angular',
   'password': 'admin',
   'scope': ''
  };

  const myFormData = this.getFormData(body);

  return this.http.post(authURL, myFormData);
}

getFormData(object) {
  const formData = new FormData();
  Object.keys(object).forEach(key => formData.append(key, object[key]));
  return formData;
}