I am getting this error when I am calling the api via Angular. However, when I call it via Postman, I am getting the desired response. I dont know what am I doing wrong. Below is the code that I have used:
generateToken() {
let serverUIUri: string;
const sessionState = sessionStorage.getItem('state');
if (sessionState === undefined || sessionState !== this.state) {
this.router.navigate(['/account/login']);
} else {
this.userService.tokenGeneration("SSO", 'authorization_code', 'http://localhost:4200', this.code).subscribe((response: any) => {
sessionStorage.setItem('demo_cookiee', response.accessToken);
this.router.navigate(['/']);
}, error => {
this.continueToAppSelection();
//this.router.navigate(['/account/login']);
});
}
}
tokenGeneration(clientId: any, authorizationCode: any, redirectUri: any, code: any): Observable<any> {
const options = {
headers: new HttpHeaders()
.append('Content-Type', 'application/x-www-form-urlencoded'),
withCredentials: true,
origin: 'http://localhost:4200/',
referer: 'http://localhost:4200/'
};
const body = {
grant_type: authorizationCode,
redirect_uri: redirectUri,
client_id: clientId,
code: code
}
return this.demohttp.postData(this.url + 'connect/token', options, body);
}
postData(url: string, headers: any, body: any): Observable<any> {
return this.http.post(url, body, headers);
}
I am passing the same parameters that are in Body and Header to postman as well. There it is successfully getting a response. However, through code it gives the below error:
Object {error: "invalid_request", error_description: "The mandatory 'grant_type' parameter is missing."}
I am passing the grant_type as 'authorization_code', it still gives the error though. Any idea what am I doing wrong?
I rectified the issue, the body object was created correctly due to which I was getting the error The mandatory 'grant_type' parameter is missing.. However after fixing the body object, it worked perfectly fine.
tokenGeneration(clientId: any, authorizationCode: any, redirectUri: any, code: any): Observable<any> {
const options = {
headers: new HttpHeaders()
.append('Content-Type', 'application/x-www-form-urlencoded'),
withCredentials: true
};
const body = 'grant_type=' + authorizationCode + '&redirect_uri=' + redirectUri + '&client_id=' + clientId + '&code=' + code;
return this.demoHttp.postDataWithOptions(this.ssoUrl + 'connect/token',options, body);
}