Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Cognito with the generated Javascript SDK?

I couldn't find any documentation that showed how to do this so I tried my best to figure it out (is this not a common use case)? I've set up my resource to use IAM authentication, set up CORS, etc. Then I deployed it, and downloaded the generated the SDK.

On the client-side I'm using the credentials from AWS.CognitoIdentityCredentials with apigClientFactory.newClient. When I try to post to my resource, I get a 403 error response with no body.

The response headers contain: x-amz-ErrorType: UnrecognizedClientException

Could this error possibly be coming from some other AWS service (do they bubble up like that)? If so, how can I tell which one? What else might be causing the error?

The code I'm using test test client-side looks like this:

function onFacebookLogin(fbtoken) {
  // get cognito credentials
  AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-1:abcd6789-1234-567a-b123-12ab34cd56ef',
    Logins: {'graph.facebook.com': fbtoken}
  });
  AWS.config.credentials.get(function(err) {
    if (err) {return console.error('Credentials error: ', err);}

    /* I'm assuming that this is what I use for accessKey and secretKey */
    var credentials = AWS.config.credentials;
    apigClient = apigClientFactory.newClient({
      accessKey: credentials.accessKeyId,
      secretKey: credentials.secretAccessKey
    });
  });
}
like image 658
Ben Davis Avatar asked Jan 14 '16 13:01

Ben Davis


People also ask

What is Cognito SDK?

Amazon Cognito Identity SDK for JavaScript Your User Pool in Amazon Cognito is a fully managed user directory that can scale to hundreds of millions of users, so you don't have to worry about building, securing, and scaling a solution to handle user management and authentication.


1 Answers

I think what might be happening is you're not setting the sessionToken field with the access key and secret key. Can you try setting it up to look like the below example and see if that works?

var client = apigClientFactory.newClient({ 
    accessKey: ACCESS_KEY, 
    secretKey: SECRET_KEY, 
    sessionToken: SESSION_TOKEN 
});

This previous question has a bit more detail, if needed.

like image 186
Jeff Bailey Avatar answered Oct 27 '22 06:10

Jeff Bailey