-
Notifications
You must be signed in to change notification settings - Fork 299
How to get the Token for API Call #106
Comments
I added the generated api sdk to the project and using that for now, i would love to see this in a more native typescript/angular way.... but it's not my expertise. |
Thank you for your answer @vbudilov . I use this function (getIdToken()) to get the token. Token that I provide in my call to the API gateway. Here the service I created: import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers} from '@angular/http';
import { Businessunit } from '../models/businessunit';
import { Observable } from 'rxjs/Observable';
import {Callback, CognitoUtil} from '../service/cognito.service';
import 'rxjs/Rx';
@Injectable()
export class BuService {
private urlapi = 'https://###';
private uri = 'businessunits';
public idToken: string;
constructor(
private http: Http,
public cognitoUtil: CognitoUtil) {
this.cognitoUtil.getIdToken(new IdTokenCallback(this));
}
// Get all the divisions
getBusinessunits(): Observable<Response> {
let header = new Headers();
header.append('Access-Control-Allow-Origin', '*');
header.append('Content-Type', 'application/json');
header.append('Authorization', this.idToken);
let options = new RequestOptions({ headers : header });
return this.http.get(this.urlapi + '/' + this.uri, options);
}
}
export class IdTokenCallback implements Callback {
constructor(public jwt: BuService) {
}
callback() {
}
callbackWithParam(result) {
this.jwt.idToken = result;
}
} This documentation helped me a lot as well to know what token I needed to provide (Access Token or ID Token) and how to setup the AWS API Gateway. Happy coding! |
@Nicosoft Thank you for sharing. The only issue I can see with this is that it will expire within an hour. |
@kgilper you are right. If you stay on the same page, the token will expired. |
@Nicosoft you could do something like this. I ran it for a few hours calling the function using a timeout. The id token was refreshed as expected and it did not make extraneous calls to amazon. import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {Callback, CognitoUtil} from '../services/cognito.service';
@Injectable()
export class BuService {
private urlapi = 'https://###';
private uri = 'businessunits';
public idToken: string;
constructor(private http: Http, public cognitoUtil: CognitoUtil, public userParams: UserParametersService) { }
getToken(): Observable<any> {
return Observable.create(observer => {
this.cognitoUtil.getIdToken(new IdTokenCallback(result => {
observer.next(result);
}));
});
}
getBusinessunits(): Observable<Response> {
return this.getToken().flatMap(token => {
const header = new Headers();
header.append('Access-Control-Allow-Origin', '*');
header.append('Content-Type', 'application/json');
header.append('Authorization', token);
const options = new RequestOptions({headers: header});
return this.http.get(this.urlapi + '/' + this.uri, options);
});
}
}
export class IdTokenCallback implements Callback {
_valueReturnFunction: any = null;
constructor(valueReturnFunction: any) {
this._valueReturnFunction = valueReturnFunction;
}
callback() {
}
callbackWithParam(result) {
this._valueReturnFunction(result);
}
} |
@kgilper , thanks for sharing. I am testing your version right now! |
@Nicosoft Did it work ok? |
@kgilper , your version works better than my one. |
Hi there,
I am trying to create a new method in /serverice/cognito.service.ts that returns the token JWT.
I need the token because I want to call a method in AWS Gateway. This method has a Authorization (Cognito User Pool).
So I wrote the following function in /serverice/cognito.service.ts

getToken() { let token = this.getCurrentUser().getSignInUserSession().getAccessToken().getJwtToken(); console.log('### TOKEN', token); return token; }
Unfortunately this generates an error:
So my question is:
How do I correctly get the Token?
Thank for your help!
The text was updated successfully, but these errors were encountered: