Wie rufe ich AWS API Gateway Endpoint mit Cognito-ID (+ Konfiguration) auf?
Ich möchte ein @ anrufAWS API Gateway Endpoint
das ist geschützt mitAWS_IAM
Verwendung dergenerated JavaScript API SDK
.
Ich habe einCognito UserPool
und einCognito Identity Pool
. Beides korrekt synchronisiert überClientId
.
Ich benutze diesen Code, umSign in
und bekomme dasCognito Identity
AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});
AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});
var poolData = {
UserPoolId: 'us-east-1_XXXXXXXX',
ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXX'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var authenticationData = {
Username: 'user',
Password: '12345678',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var userData = {
Username: 'user',
Pool: userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log('access token + ' + result.getAccessToken().getJwtToken());
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXX',
IdentityId: AWS.config.credentials.identityId,
Logins: {
'cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXX': result.idToken.jwtToken
}
});
AWS.config.credentials.get(function (err) {
// now I'm using authenticated credentials
if(err)
{
console.log('error in autheticatig AWS'+err);
}
else
{
console.log(AWS.config.credentials.identityId);
}
});
},
onFailure: function (err) {
alert(err);
}
});
All das gelingt und ich habe einauthorized Cognito Identity
jetzt
Nun versuche ich das @ anzurufAPI Gateway Endpoint
um das @ auszuführLambda Function
es zeigt auf.
var apigClient = apigClientFactory.newClient({
accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY',
secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY',
sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token
region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
});
var params = {
// This is where any modeled request parameters should be added.
// The key is the parameter name, as it is defined in the API in API Gateway.
};
var body = {
// This is where you define the body of the request,
query: '{person {firstName lastName}}'
};
var additionalParams = {
// If there are any unmodeled query parameters or headers that must be
// sent with the request, add them here.
headers: {},
queryParams: {}
};
apigClient.graphqlPost(params, body, additionalParams)
.then(function (result) {
// Add success callback code here.
console.log(result);
}).catch(function (result) {
// Add error callback code here.
console.log(result);
});
Aber dies schlägt leider fehl. DasOPTIONS
Anfrage erfolgreich mit200
aber diePOST
scheitert dann mit403
.
Ich bin mir ziemlich sicher, dass es kein @ giCORS
Problem hier.
Ich bin mir ziemlich sicher, dass das Problem mit @ zu tun hIAM Roles
undAWS Resource Configurations
.
Meine Frage ist im Grunde, können Sie mir bitte alle notwendigenAWS Resource Configurations
undIAM Roles
das sind nötig damit das funktioniert bitte?
Ressourcen, die ich habe, sind
API Gateway - mit implementierten API-Endpunkten Lambda-Funktion - vom Endpunkt aufgerufenCognito User Pool - mit App, die mit dem Identitätspool synchronisiert istCognito Identity Pool - mit zugeordneter autorisierter und nicht autorisierter Rolle.IAM-Rollen - für die Lambda-Funktion und die autorisierte und nicht autorisierte Rolle des Cognito Identity Pools.Aber ich weiß nicht, wie diese Ressourcen richtig konfiguriert werden müssen, damit dies funktioniert.
Vielen Dan