繁体   English   中英

Amazon Cognito JavaScript Java SDK记录无效的安全令牌

[英]Amazon Cognito javascript sdk listrecords invalid security token

我正在尝试学习如何使用Node.js和Amazon Cognito。 我使用的是开发人员验证的身份,而不是使用google,facebook等。 我已经完成了这一部分,可以看到在cognito中创建的身份。 当我尝试运行listRecords时,尽管它不起作用。 我只想将用户名添加到数据集中即可查看它的工作原理。 它给我下面的错误。 我不太确定安全令牌的含义,因为开发人员访问权限和秘密密钥可以很好地用于使identityId脱离cognito。

undefinedUnrecognizedClientException: The security token included in the request is invalid. UnrecognizedClientException: The security token included in the request is invalid.

我正在使用的代码在下面。 该错误发生在listRecords上,并且没有执行错误功能。 据我所知,调用listRecords失败。 我也仔细检查了访问键。

// initialize the Credentials object
            AWS.config.credentials = new AWS.CognitoIdentity(params);

            AWS.config.credentials.getOpenIdTokenForDeveloperIdentity(params, function (err, data) {
                if (err) console.log("## credentials.get: ".red + err, err.stack);
                else {

                    AWS.config.credentials.identityId = data.IdentityId;
                    console.log(data);
                    var cognitosync = new AWS.CognitoSync();
                    cognitosync.listRecords({
                        DatasetName: "userData",
                        IdentityId: AWS.config.credentials.identityId,
                        IdentityPoolId: IDENTITY_POOL,
                    }, function (err, data) {
                        if (err) console.log("## listRecords: ".red + err, err.stack); // an error occurred
                        else {
                            console.log("This is the sync session token: " + data.SyncSessionToken);

                            //Parameters for updating the dataset
                            var params = {
                                DatasetName: "userData",
                                IdentityId: AWS.config.credentials.identityId,
                                IdentityPoolId: IDENTITY_POOL,
                                RecordPatches: [{
                                    Key: 'UserName',
                                    Op: 'replace',
                                    SyncCount: data.DatasetSyncCount,
                                    Value: 'FirstName' //this needs to be tied into first and last name
                                }]
                            };

                            //Make the call to Amazon Cognito
                            cognitosync.updateRecords(params, function (err, data) {
                                if (err) {
                                    console.log("## updateRecords: ".red + err, err.stack);
                                } // an error occurred
                                else {
                                    var dataRecords = JSON.stringify(data);
                                }
                            });
                        }

                    });

                }

该文档可能对您有所帮助: http : //docs.aws.amazon.com/cognito/devguide/identity/developer-authenticated-identities/

一般来说

(1)您将要首先使用通过AWS.CognitoIdentity来获取openIdToken来调用GetOpenIdTokenForDeveloperIdentity。

在上述文档的“获取令牌(服务器端)”部分中对此进行了描述。

(2)从上方获得令牌后,将AWS.config.credentials设置为:

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
   IdentityPoolId: 'IDENTITY_POOL_ID',
   IdentityId: 'IDENTITY_ID_RETURNED_FROM_YOUR_PROVIDER',
   Logins: {
      'cognito-identity.amazonaws.com': 'TOKEN_RETURNED_FROM_YOUR_PROVIDER'
   }
});

然后,您应该能够使用用户的凭据进行呼叫。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM