Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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
Php 在AWS中使用cognito调用未定义的成员函数adminInitiateAuth()_Php_Laravel_Amazon Cognito_Aws Sdk - Fatal编程技术网

Php 在AWS中使用cognito调用未定义的成员函数adminInitiateAuth()

Php 在AWS中使用cognito调用未定义的成员函数adminInitiateAuth(),php,laravel,amazon-cognito,aws-sdk,Php,Laravel,Amazon Cognito,Aws Sdk,我通过创建下面给出的函数来使用相同的代码: $this->client = new CognitoIdentityProviderClient([ 'version' => '2016-04-18', 'region' => 'us-east-1', ]); $result = $this->client->adminInitiateAuth([ 'AuthFlow' => 'ADMIN_NO_SRP_AUTH', 'ClientId'

我通过创建下面给出的函数来使用相同的代码:

$this->client = new CognitoIdentityProviderClient([
  'version' => '2016-04-18',
  'region' => 'us-east-1',
]);
$result = $this->client->adminInitiateAuth([
    'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
    'ClientId' => $this->client_id,
    'UserPoolId' => $this->userpool_id,
    'AuthParameters' => [
        'USERNAME' => 'xxxx',
        'PASSWORD' => 'xxxxxx',
    ],
]);
我创建了一个RESTAPI调用并调用了这个函数。我收到对未定义成员adminInitiateAuth的错误调用。我已经在.env fileClientID、Region、userpoolID中声明了凭据

我已初始化$this->client,如下所示:

public function authenticate(string $username, string $password) : string
    {
        try {
            $result = $this->client->adminInitiateAuth([
                'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
                'ClientId' => $this->client_id,
                'UserPoolId' => $this->userpool_id,
                'AuthParameters' => [
                    'USERNAME' => $username,
                    'PASSWORD' => $password,
                ],
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        $this->setAuthenticationCookie($result->get('AuthenticationResult')['AccessToken']);

        return '';
    }
请查找完整的课程代码:

public function initialize() : void
    {
        $this->client = new CognitoIdentityProviderClient([
          'version' => '2016-04-18',
          'region' => $this->region,
        ]);

        try {
            $this->user = $this->client->getUser([
                'AccessToken' => $this->getAuthenticationCookie()
            ]);
        } catch(\Exception  $e) {
            // an exception indicates the accesstoken is incorrect - $this->user will still be null
        }
    }

在我看来,您使用的软件包不正确

请尝试以下操作:

<?php
namespace AWSCognitoApp;

use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;

class AWSCognitoWrapper
{
    private const COOKIE_NAME = 'aws-cognito-app-access-token';

    private $region;
    private $client_id;
    private $userpool_id;

    private $client;

    private $user = null;

    public function __construct()
    {
        if(!getenv('REGION') || !getenv('CLIENT_ID') || !getenv('USERPOOL_ID')) {
            throw new \InvalidArgumentException("Please provide the region, client_id and userpool_id variables in the .env file");
        }

        $this->region = getenv('REGION');
        $this->client_id = getenv('CLIENT_ID');
        $this->userpool_id = getenv('USERPOOL_ID');
        $this->client = new CognitoIdentityProviderClient([
            'version' => '2016-04-18',
            'region' => $this->region,
          ]);
    }

    /*public function initialize() : void
    {
        $this->client = new CognitoIdentityProviderClient([
          'version' => '2016-04-18',
          'region' => $this->region,
        ]);

        try {
            $this->user = $this->client->getUser([
                'AccessToken' => $this->getAuthenticationCookie()
            ]);
        } catch(\Exception  $e) {
            // an exception indicates the accesstoken is incorrect - $this->user will still be null
        }
    }*/

    public function authenticate(string $username, string $password) : string
    {
        try {
            $result = $this->client->adminInitiateAuth([
                'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
                'ClientId' => $this->client_id,
                'UserPoolId' => $this->userpool_id,
                'AuthParameters' => [
                    'USERNAME' => $username,
                    'PASSWORD' => $password,
                ],
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        $this->setAuthenticationCookie($result->get('AuthenticationResult')['AccessToken']);

        return '';
    }

    public function signup(string $username, string $email, string $password) : string
    {
        try {
            $result = $this->client->signUp([
                'ClientId' => $this->client_id,
                'Username' => $username,
                'Password' => $password,
                'UserAttributes' => [
                    [
                        'Name' => 'name',
                        'Value' => $username
                    ],
                    [
                        'Name' => 'email',
                        'Value' => $email
                    ]
                ],
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        return '';
    }

    public function confirmSignup(string $username, string $code) : string
    {
        try {
            $result = $this->client->confirmSignUp([
                'ClientId' => $this->client_id,
                'Username' => $username,
                'ConfirmationCode' => $code,
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        return '';
    }

    public function sendPasswordResetMail(string $username) : string
    {
        try {
            $this->client->forgotPassword([
                'ClientId' => $this->client_id,
                'Username' => $username
            ]);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        return '';
    }

    public function resetPassword(string $code, string $password, string $username) : string
    {
        try {
            $this->client->confirmForgotPassword([
                'ClientId' => $this->client_id,
                'ConfirmationCode' => $code,
                'Password' => $password,
                'Username' => $username
            ]);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        return '';
    }

    public function isAuthenticated() : bool
    {
        return null !== $this->user;
    }

    public function getPoolMetadata() : array
    {
        $result = $this->client->describeUserPool([
            'UserPoolId' => $this->userpool_id,
        ]);

        return $result->get('UserPool');
    }

    public function getPoolUsers() : array
    {
        $result = $this->client->listUsers([
            'UserPoolId' => $this->userpool_id,
        ]);

        return $result->get('Users');
    }

    public function getUser() : ?\Aws\Result
    {
        return $this->user;
    }

    public function logout()
    {
        if(isset($_COOKIE[self::COOKIE_NAME])) {
            unset($_COOKIE[self::COOKIE_NAME]);
            setcookie(self::COOKIE_NAME, '', time() - 3600);
        }
    }

    private function setAuthenticationCookie(string $accessToken) : void
    {
        /*
         * Please note that plain-text storage of the access token is insecure and
         * not recommended by AWS. This is only done to keep this example
         * application as easy as possible. Read the AWS docs for more info:
         * http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html
        */
        setcookie(self::COOKIE_NAME, $accessToken, time() + 3600);
    }

    private function getAuthenticationCookie() : string
    {
        return $_COOKIE[self::COOKIE_NAME] ?? '';
    }
}

我找到了一个您可能想查看的引用。

在调用authenticate之前是否启动了$this->client?是@AgeValed。公共函数初始化:void{$this->client=new CognitoIdentityProviderClient['version'=>'2016-04-18','region'=>$this->region,];尝试{$this->user=$this->client->getUser['AccessToken'=>$this->getAuthenticationCookie];}catch\Exception$e{//Exception表示accesstoken不正确-$this->user仍然为null}},所以您正在执行以下操作$test=新类名$测试->初始化//或者可以从_构造函数$test->authenticate'user','pass'调用此函数@IMLOKESH尝试公共函数构造而不是公共函数初始化。我也在u构造下检查过。但同样的错误也发生了@亚历克斯哈里斯
    $this->client = CognitoIdentityProviderClient::factory([
        'region' => $region,
        'version' => $version
    ]);

    $result = $this->client->adminInitiateAuth([
        'UserPoolId'     => $this->poolId,
        'ClientId'       => $this->clientId,
        'AuthFlow'       => 'ADMIN_NO_SRP_AUTH', // this matches the 'server-based sign-in' checkbox setting from earlier
        'AuthParameters' => [
            'USERNAME' => $username,
            'PASSWORD' => $password
        ]
    ]);