2
I have a simple Angular application and need to make a call to my API to get one token. But I am facing difficulties. I will try to explain a little about my flow.
I created a service class where I only ask her once to store a token. This token comes from an API, but this API does not return me a token on every call, because this token has a lifespan of 15min. When the API returns a token to me, it comes in this format:
{access_token: "32-GaypcnKItxyYBhc9HRINLebbzuoWul9q0zNitHq0ZVIcVGB…AV2LoFg3uoY6xNIMExkKEbGyda_5Z0RXFnKdi6UsBPH5b-Lwa", token_type: "bearer", expires_in: 3599}
If it comes in this format, my intention is to take this token received by the API and store it within my class said above.
If the API does not return a Token to me, the answer comes in this format:
Response {_body: "{"error":"deny","error_description":"permitido som… autenticação por aplicação à cada 15 minuto(s)"}", status: 400, ok: false, statusText: "Bad Request", headers: Headers, …}
Informing by _body, an error described: 'allowed sound... authentication per application every 15 minute(s)'.
If it comes in this format, my intention is to enter the class I entered and get the token that is in the class said above.
When the API returns a Token, I can pick it up. But when it does not return a Token, I cannot catch the token that is stored in the class.
Could someone please help me? Down here the trial code
Service class
ObterToken(): Observable<any> {
const header = new Headers();
header.append('Content-Type', 'application/x-www-form-urlencoded');
const bodyOptions = {
grant_type: 'password',
username: 'varejo_user',
password: 'w6h5xgtl'
}
const body = `grant_type=${bodyOptions.grant_type}&username=${bodyOptions.username}&password=${bodyOptions.password}`;
return this.http.post(`${ApiDeSegurança}`, body, new RequestOptions({headers: header}))
.map(response=> response.json());
}
ObtendoToken(): string {
return this.tokenModel.token;
}
AdicionandoToken(token: string): void {
this.tokenModel.token = token;
}
Component executing the service class method
ngOnInit(): void {
this.apiDeSegurancaService.ObterToken().subscribe(res => this.token = res);
}
Edited question
Worked that way:
ngOnInit(): void {
this.apiDeSegurancaService.ObterToken().subscribe( res => {
this.apiDeSegurancaService.AdicionandoToken(res.access_token);
}, error => {
this.token = this.apiDeSegurancaService.ObtendoToken();
});
}
However, I need to perform another method with this token, so I need to perform this other method only when this first request is answered! How can I do that?
Thank you in advance.
by returning 400 the angular will treat as error, in this case instead of treating the subscribe vc you have to use the cathError, https://www.learnrxjs.io/operators/error_handling/catch.html
– Eduardo Vargas
ngOnInit(): void { this.apiDegurancaService.Get token(). subscribe( res => { this.token = res } , error => { this.error = error } ); } .
– Lucas Brogni
@Lucasbrogni, that way I got the token. I posted the ngOnInit code in an issue of the question. Thank you. But I have another question. I need to use this token to perform another method, now GET, passing the token as parameter... I will describe it better in the question edition. But anyway, thank you...
– Guilherme Nunes
William, I will put my answer there as an answer, if you can mark it as an answer. Hugs.
– Lucas Brogni