KevinTale
KevinTale

Reputation: 1858

error : "invalid_grant" Angular 5

I'm trying to receive an access token from Drupal using Angular on front.

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);

}

I tried with postman and it works.

enter image description here

I tried both "" and null for scope key. Neither works.

I'm getting this error :

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"

And here is the details of what I'm sending :

enter image description here

Thank you for your help!

Upvotes: 0

Views: 1149

Answers (1)

KevinTale
KevinTale

Reputation: 1858

I managed to solve the issue.

The POST Request was not expected Json but FormData. So I had to convert my Json to FormData like so :

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;
}

Upvotes: 1

Related Questions