Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cognito ListUsers在Angular 2中的功能_Angular_Amazon Web Services_Angular2 Services_Amazon Cognito_Aws Sdk Js - Fatal编程技术网

Cognito ListUsers在Angular 2中的功能

Cognito ListUsers在Angular 2中的功能,angular,amazon-web-services,angular2-services,amazon-cognito,aws-sdk-js,Angular,Amazon Web Services,Angular2 Services,Amazon Cognito,Aws Sdk Js,我试图在Angular 2中为AWS Cognito使用listUsers函数。每次调用函数时,我都会收到一个错误,指出listUsers不是函数 我正在使用AWS Cognito Quickstart for Angular 2()示例添加到Cognito.service.ts文件中,以尝试合并listUsers功能 用于Cognito()的Javascript SDK的文档说明了如下调用函数: var params = { UserPoolId: 'STRING_VALUE', /* r

我试图在Angular 2中为AWS Cognito使用listUsers函数。每次调用函数时,我都会收到一个错误,指出listUsers不是函数

我正在使用AWS Cognito Quickstart for Angular 2()示例添加到Cognito.service.ts文件中,以尝试合并listUsers功能

用于Cognito()的Javascript SDK的文档说明了如下调用函数:

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
});
我尝试过在cognito.service.ts中使用listUsers函数,如下所示:

@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.\u POOL\u数据)


更新

我已将
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<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
o@http://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
e@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<.getCredentials@http://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<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<.emit@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<.emitEvent@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/t@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</n.prototype.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<.send@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<.makeRequest@http://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
CognitoUtil.prototype.getList@http://localhost:3000/main.bundle.js:436:10
organizationsViewComponent.prototype.viewUsersInOrg@http://localhost:3000/main.bundle.js:1435:22
anonymous/_View_organizationsViewComponent1.prototype._handle_click_13_0@organizationsViewComponent.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
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<.onInvoke@http://localhost:3000/vendor.bundle.js:6256:33
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14767:18
Zone.prototype.runGuarded@http://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</NgZoneImpl.prototype.runInnerGuarded@http://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</NgZone.prototype.runGuarded@http://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler@http://localhost:3000/vendor.bundle.js:11945:79
ZoneDelegate.prototype.invokeTask@http://localhost:3000/polyfills.bundle.js:14801:22
Zone.prototype.runTask@http://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/this.invoke@http://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<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
o@http://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
e@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<.getCredentials@http://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<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<.emit@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<.emitEvent@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/t@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</n.prototype.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<.send@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<.makeRequest@http://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
CognitoUtil.prototype.getList@http://localhost:3000/main.bundle.js:436:10
organizationsViewComponent.prototype.viewUsers@http://localhost:3000/main.bundle.js:1435:22
anonymous/_View_organizationsViewComponent1.prototype._handle_click_13_0@organizationsViewComponent.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
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<.onInvoke@http://localhost:3000/vendor.bundle.js:6256:33
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14767:18
Zone.prototype.runGuarded@http://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</NgZoneImpl.prototype.runInnerGuarded@http://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</NgZone.prototype.runGuarded@http://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler@http://localhost:3000/vendor.bundle.js:11945:79
ZoneDelegate.prototype.invokeTask@http://localhost:3000/polyfills.bundle.js:14801:22
Zone.prototype.runTask@http://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/this.invoke@http://localhost:3000/polyfills.bundle.js:14871:29
ConfigError:配置中缺少区域
堆栈跟踪:

_xamzrequire您正在对类型为:
AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool
的对象调用listUsers,您需要在类型为:
AWS的对象上调用它。CognitoIdentityServiceProvider
CognitoUserPool
没有此类方法。

listUsers操作通过主AWS SDK公开,它是一个经过身份验证的操作,意味着需要AWS凭据才能运行。节点中的一些代码:

var aws = require('aws-sdk');
aws.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});

var CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
var client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: 'us-east-1' });

//now you can call listUsers on the client object     

请注意,您可以通过不同的方式配置AWS凭据以调用该操作。您确实需要凭据,因为这是一个经过身份验证的操作。CreateUserPool与此类似,您只需要在调用中以JSON的形式传递适当的参数。

我将
AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool
更改为
AWS.CognitoIdentityServiceProvider
,现在我收到错误
ConfigError:config堆栈跟踪中缺少区域:xamzrequireMy应用程序是一个Angular 2 web应用程序,我我不确定是否应该通过客户端/前端分发SecretAccessKey。我不是使用node作为后端,而是使用Java。此呼叫是否可以直接在前端进行,而不是使用secretAccessKey,而是使用令牌?此呼叫不能使用令牌进行。如果您正在验证用户并获取令牌,则可以使用Cognito Federated Identifications获取临时AWS凭据,该凭据可用于使用主AWS SDK调用listUsers(下面repo中的用例17)。感谢您的帮助。