我尝试使用javascript sdk为AWS调用列表用户API,但在调用API时出错。
Code:
// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'ap-northeast-2'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'my identity pool id',
});
AWS.config.accessKeyId = 'my access key id';
AWS.config.secretAccessKey = 'my secret access key';
var cognitoidentityserviceprovider = new
AWS.CognitoIdentityServiceProvider();
var myListUsers = function() {
var myparams = {
UserPoolId: 'my user pool id', /* required */
};
cognitoidentityserviceprovider.listUsers(myparams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
响应:
TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
at d (aws-sdk-2.144.0.min.js:47)
at o (aws-sdk-2.144.0.min.js:47)
at new i (aws-sdk-2.144.0.min.js:47)
at Object.update (aws-sdk-2.144.0.min.js:47)
at Object.hmac (aws-sdk-2.144.0.min.js:46)
at Object.getSigningKey (aws-sdk-2.144.0.min.js:46)
at constructor.signature (aws-sdk-2.144.0.min.js:46)
at constructor.authorization (aws-sdk-2.144.0.min.js:46)
at constructor.addAuthorization (aws-sdk-2.144.0.min.js:46)
at aws-sdk-2.144.0.min.js:43 "TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.
at d (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:47:6802)
at o (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:47:5442)
at new i (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:47:5222)
at Object.update (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:47:25616)
at Object.hmac (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:46:24139)
at Object.getSigningKey (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:46:16036)
at constructor.signature (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:46:13643)
at constructor.authorization (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:46:13581)
at constructor.addAuthorization (https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:46:12314)
at https://sdk.amazonaws.com/js/aws-sdk-2.144.0.min.js:43:13208"
的示例
我使用的代码与示例中的代码相同。 任何人都可以提出纠正错误的建议吗?
提前谢谢。
答案 0 :(得分:0)
尝试删除此位:
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'my identity pool id',
});
我测试了您发布的代码(使用节点9.1.0和aws-sdk
2.149.0)并得到了一个不同的但非常相似的错误:
{ TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView
at new Hmac (internal/crypto/hash.js:84:11)
at Object.createHmac (crypto.js:122:10)
at Object.hmac (D:\codecommit\cognitotest\node_modules\aws-sdk\lib\util.js:401:30)
at Object.getSigningKey (D:\codecommit\cognitotest\node_modules\aws-sdk\lib\signers\v4_credentials.js:59:8)
我删除了上面的三行,事情很顺利。