x-amzn-ErrorType:UnrecognizedClientException While Calling AWS Api gateway with temporary Credentials

Viewed 1950

After successful Authentication with Cognito User Pool I got temporary credentials to access aws resources

               var akey='';
               var skey='';
               var st='';

               AWS.config.credentials.get(function (err) {
                       akey = AWS.config.credentials.accessKeyId;
                       skey = AWS.config.credentials.secretAccessKey;
                       st = AWS.config.credentials.sessionToken;
                       var identityId = AWS.config.credentials.identityId;

                        });

Using those Credentials I called API which was using IAM Authorization

function ViewMyBookings_With_Credentials() {
debugger;
var apigClient = apigClientFactory.newClient({
     accessKey: akey,
     secretKey: skey,
     sessionToken: st,
     region: 'ap-south-1'
});
var params = {    };
var body = ''; // Get Request
var additionalParams = {      };
apigClient.mybookingsGet(params, body, additionalParams)
    .then(function (result) {
        //This is where you would put a success callback
        alert(result.data);
    }).catch(function (result) {
        debugger;
        alert(result.data);
        //This is where you would put an error callback
    });

}

Options Request Successful and I got 200 Ok For that But for Actual Request is showing CORS Error,I am sure that there is no issue with CORS. Response Headers are showing

x-amzn-ErrorType:UnrecognizedClientException X-Cache:Error from cloudfront

I searched on AWS documentation for this but not received any positive response.

One Question addressing similar issue

But the answer is not correct/complete, Please let me know what I am missing .

2 Answers

Go to API Gateway > OPTIONS (for resource of your GET method) > Header Mappings >

  • Access-Control-Allow-Headers 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-XSRF-TOKEN,Access-Control-Allow-Headers,Access-Control-Allow-Origin'
  • Access-Control-Allow-Methods 'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'
  • Access-Control-Allow-Origin '*' Let me know if you need more details.

This question is related to the javascript code. In my case, I'm using python and calling a secured API. Got credentials successfully. But getting the exact specified error. So answering my problem, it will be useful if somebody falls here with same error.

in post call I have to use auth=authenticaion() which I am doing like this
authenticaion() function returns the following

return AWS4Auth(temp_credentials['AccessKeyId'],
                    temp_credentials['SecretAccessKey'],
                    'us-west-2',
                    'call-api',
                    temp_credentials['SessionToken'])

But for AWS4Auth session-token is mandatory arugment. And we have to specify it. So Below code solved my problem

return AWS4Auth(temp_credentials['AccessKeyId'],
                    temp_credentials['SecretAccessKey'],
                    'us-west-2',
                    'call-api',
                    session_token=temp_credentials['SessionToken'])
Related