0

I am using the generated JS SDK by API Gateway. Problem is when theres errors, I get a CORS error and I am unable to get the data inside nor the status code:

enter image description here

Notice data is "" despite there being a message in the network tab

enter image description here

My code:

AWS.config.region = "ap-northeast-2"
const cognitoParams = {
  IdentityPoolId: "ap-northeast-2:...",
  Logins: {
    "accounts.google.com": googleUser.getAuthResponse().id_token
  }
}

AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

AWS.config.credentials.get(function () {

  const api = apigClientFactory.newClient({
    accessKey: AWS.config.credentials.accessKeyId,
    secretKey: AWS.config.credentials.secretAccessKey,
    sessionToken: AWS.config.credentials.sessionToken
  })
  api.ec2Get()
    .then(function (data) {
      console.log('data', data);
    })
    .catch(function (err) {
      console.error('err', err)
    })
3
  • you are sure that AWS.CognitoIdentityCredentials is really returning credentials? Commented May 27, 2017 at 13:56
  • @UXDart yes there's data in access key and secret access key. Or what else should I verify? Commented May 27, 2017 at 15:13
  • Are you sure you enabled CORS for your API Gateway resource and deployed your API to stage? Commented May 28, 2017 at 7:11

1 Answer 1

1

try to pass the region too... so inside:

const api = apigClientFactory.newClient({
  accessKey: AWS.config.credentials.accessKeyId,
  secretKey: AWS.config.credentials.secretAccessKey,
  sessionToken: AWS.config.credentials.sessionToken,
  region: '...'
})

because by default it uses 'us-east-1' if you don't pass it

Sign up to request clarification or add additional context in comments.

9 Comments

Hi this solves the problem of having an invalid security token, but I find that when the token expires, I am still unable to get the error message nor status via JS since its a CORS error. Any ideas?
ok, but the CORS error could be because there is indeed a CORS error, or because the server returned a status different than 200.. what do you see in the console? only the CORS error? if you receive a status 403, then you know the token is expired
How do I get the HTTP status from the error from SDK generated from API gateway. As you can see from the screenshot, status is 0?
.catch(function (err) { if(err.status===403) { }) you need to check in the case of an error...
But the problem is err.status is 0 in my case as shown in the screenshot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.