我想在Angular 2中爲AWS Cognito使用listUsers函數。每次調用函數時,都會收到錯誤消息,指出listUsers不是函數。Cognito ListUsers在Angular 2中的功能
我正在使用適用於Angular 2的AWS Cognito快速入門(https://github.com/awslabs/aws-cognito-angular2-quickstart)示例我正在添加到cognito.service.ts文件以嘗試合併listUsers功能。
爲的Javascript SDK的Cognito的文檔(http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property)說調用類似如下的功能:
var params = {
UserPoolId: 'STRING_VALUE', /* required */
AttributesToGet: [
'STRING_VALUE',
/* more items */
],
Filter: 'STRING_VALUE',
Limit: 0,
PaginationToken: 'STRING_VALUE'
};
cognitoidentityserviceprovider.listUsers(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
我曾嘗試使用listUsers功能cognito.service.ts這樣的:
@Injectable()
export class CognitoUtil {
public static _REGION = environment.region;
public static _IDENTITY_POOL_ID = environment.identityPoolId;
public static _USER_POOL_ID = environment.userPoolId;
public static _CLIENT_ID = environment.clientId;
public static _POOL_DATA = {
UserPoolId: CognitoUtil._USER_POOL_ID,
ClientId: CognitoUtil._CLIENT_ID
};
public static getAwsCognito(): any {
return AWSCognito;
}
getUserPool() {
return new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA);
}
getCurrentUser() {
return this.getUserPool().getCurrentUser();
}
getList(myparams){
var params = {
UserPoolId: environment.userPoolId, /* required */
AttributesToGet: [],
Filter: "",
Limit: 10
};
var cognito = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA);
cognito.listUsers(params, function(err, data) {
if (err) console.log(err, err.stack);// an error occurred
else return data;// successful response
});
}
getCognitoIdentity(): string {
return AWS.config.credentials.identityId;
}
getAccessToken(callback: Callback): void {
if (callback == null) {
throw("CognitoUtil: callback in getAccessToken is null...returning");
}
if (this.getCurrentUser() != null)
this.getCurrentUser().getSession(function (err, session) {
if (err) {
console.log("CognitoUtil: Can't set the credentials:" + err);
callback.callbackWithParam(null);
}
else {
if (session.isValid()) {
callback.callbackWithParam(session.getAccessToken().getJwtToken());
}
}
});
else
callback.callbackWithParam(null);
}
getIdToken(callback: Callback): void {
if (callback == null) {
throw("CognitoUtil: callback in getIdToken is null...returning");
}
if (this.getCurrentUser() != null)
this.getCurrentUser().getSession(function (err, session) {
if (err) {
console.log("CognitoUtil: Can't set the credentials:" + err);
callback.callbackWithParam(null);
}
else {
if (session.isValid()) {
callback.callbackWithParam(session.getIdToken().getJwtToken());
} else {
console.log("CognitoUtil: Got the id token, but the session isn't valid");
}
}
});
else
callback.callbackWithParam(null);
}
getRefreshToken(callback: Callback): void {
if (callback == null) {
throw("CognitoUtil: callback in getRefreshToken is null...returning");
}
if (this.getCurrentUser() != null)
this.getCurrentUser().getSession(function (err, session) {
if (err) {
console.log("CognitoUtil: Can't set the credentials:" + err);
callback.callbackWithParam(null);
}
else {
if (session.isValid()) {
callback.callbackWithParam(session.getRefreshToken());
}
}
});
else
callback.callbackWithParam(null);
}
refresh(): void {
this.getCurrentUser().getSession(function (err, session) {
if (err) {
console.log("CognitoUtil: Can't set the credentials:" + err);
}
else {
if (session.isValid()) {
console.log("CognitoUtil: refreshed successfully");
} else {
console.log("CognitoUtil: refreshed but session is still not valid");
}
}
});
}
}
這是我如何在組件調用的GetList(PARAMS):
constructor(public cognitoUtil: CognitoUtil){}
viewUsers(){
console.log(this.cognitoUtil.getList(this.param));
}
我嘗試了多次不同的事情來得到這個工作,我似乎無法得到它的工作。我嘗試過ListUsers而不是ListUsers,並且我嘗試調用getUserPool()而不是使用AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA)。
更新
我已經改變AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool
到AWS.CognitoIdentityServiceProvider
像建議。我現在收到的錯誤:類型的對象上
ConfigError: Missing region in config
Stack trace:
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11200
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21745
_xamzrequire<[33]</n.EventListeners.Core</</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11061
[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24648
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:10913
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:27369
_xamzrequire<[59]</<.defineMethods/</t.prototype[e]@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:30572
[email protected]://localhost:3000/main.bundle.js:436:10
[email protected]://localhost:3000/main.bundle.js:1435:22
anonymous/[email protected]rganizationsViewComponent.ngfactory.js:1991:15
[2]/</</DebugAppView</DebugAppView.prototype.eventHandler/<@http://localhost:3000/vendor.bundle.js:9712:29
decoratePreventDefault/<@http://localhost:3000/vendor.bundle.js:11832:41
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler/<@http://localhost:3000/vendor.bundle.js:11945:116
[email protected]://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<[email protected]://localhost:3000/vendor.bundle.js:6256:33
[email protected]://localhost:3000/polyfills.bundle.js:14767:18
[email protected]://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</[email protected]://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</[email protected]://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/[email protected]://localhost:3000/vendor.bundle.js:11945:79
[email protected]://localhost:3000/polyfills.bundle.js:14801:22
[email protected]://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/[email protected]://localhost:3000/polyfills.bundle.js:14871:29
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11200
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21745
_xamzrequire<[33]</n.EventListeners.Core</</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11061
[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24648
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:10913
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<[email protected]://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:27369
_xamzrequire<[59]</<.defineMethods/</t.prototype[e]@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:30572
[email protected]://localhost:3000/main.bundle.js:436:10
[email protected]://localhost:3000/main.bundle.js:1435:22
anonymous/[email protected]rganizationsViewComponent.ngfactory.js:1991:15
[2]/</</DebugAppView</DebugAppView.prototype.eventHandler/<@http://localhost:3000/vendor.bundle.js:9712:29
decoratePreventDefault/<@http://localhost:3000/vendor.bundle.js:11832:41
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler/<@http://localhost:3000/vendor.bundle.js:11945:116
[email protected]://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<[email protected]://localhost:3000/vendor.bundle.js:6256:33
[email protected]://localhost:3000/polyfills.bundle.js:14767:18
[email protected]://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</[email protected]://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</[email protected]://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/[email protected]://localhost:3000/vendor.bundle.js:11945:79
[email protected]://localhost:3000/polyfills.bundle.js:14801:22
[email protected]://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/[email protected]://localhost:3000/polyfills.bundle.js:14871:29
我的應用程序是一個Angular 2 Web應用程序,我不確定secretAccesKey是否應通過客戶端/前端進行分發。我不使用節點作爲我的後端,我正在使用Java。這個調用可以直接在前端進行,而不是使用secretAccessKey而是使用令牌? – JSONGagnon
此調用不能使用令牌。如果您正在對用戶進行身份驗證並獲取令牌,則可以使用Cognito聯合身份來獲取臨時AWS憑證,該憑證可用於使用主AWS SDK(以下回購協議中的用例17)調用listUsers https:// github。com/aws/amazon-cognito-identity-js –
謝謝你的幫助。 – JSONGagnon