Amazon Cognito, API Gateway and S3 integration

0

Hello,

I've been trying for way too much time to call my POST API Gateway endpoint from my plain web page stored in S3.
I get this error and I understand that I need to add that Authentication header
x-amzn-errortype: MissingAuthenticationTokenException
I was following this tutorial https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-lambda-browser-script.html My question is how to use my Amazon Cognito to sign my requests to my API Gateway? I have CORS enabled on my POST endpoint and I can access it via Postman. I configured it to sign my requests with an AWS Signature based on my AccessKey and SecretKey.

Kind regards,
Tudor

asked 4 years ago303 views
1 Answer
0

I managed to solve it by generating the api Gateway client. First step was to deploy my API in the Gateway portal. Second step is to go to the stages section, select the one where you deployed it and go to the SDK Generation tab, select your platform (in my case Javascript) and press the Generate SDK button.

Then I imported those things in my code.

    <script type="text/javascript" src="lib/axios/dist/axios.standalone.js"></script>
    <script type="text/javascript" src="lib/CryptoJS/rollups/hmac-sha256.js"></script>
    <script type="text/javascript" src="lib/CryptoJS/rollups/sha256.js"></script>
    <script type="text/javascript" src="lib/CryptoJS/components/hmac.js"></script>
    <script type="text/javascript" src="lib/CryptoJS/components/enc-base64.js"></script>
    <script type="text/javascript" src="lib/url-template/url-template.js"></script>
    <script type="text/javascript" src="lib/apiGatewayCore/sigV4Client.js"></script>
    <script type="text/javascript" src="lib/apiGatewayCore/apiGatewayClient.js"></script>
    <script type="text/javascript" src="lib/apiGatewayCore/simpleHttpClient.js"></script>
    <script type="text/javascript" src="lib/apiGatewayCore/utils.js"></script>
    <script type="text/javascript" src="apigClient.js"></script>

You instantiate your api Client

apigClient = apigClientFactory.newClient({
                accessKey: &#39;some access key&#39;,
                secretKey: &#39;some secret key&#39;,
                region: AWS.config.region
            });

You use it like so:

var body = {};

var params = {
        subscriber: email
}
var additionalParams = {}

apigClient.subscribersDevicesGet(params, body, additionalParams)
        .then(function(result){
            // clear form and show a success message
            alert("Successful");
            addSubscriberDevicesToForm(result.data.devices);
        }).catch( function(result){
            // show an error message
            alert("Loading of devices failed: " + result);
        });

where subscribersDevicesGet is my GET endpoint for /subscribers/devices endpoint
In the autocreated apigClient.js you have all of the endpoints defined

answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions