luchianenco/oauth2-amazon

为PHP League OAuth2-Client提供的Amazon OAuth 2.0客户端提供者

3.0.0 2022-10-28 13:46 UTC

This package is not auto-updated.

Last update: 2024-09-13 22:26:23 UTC


README

Latest Version Build Status Total Downloads

此包为PHP League的OAuth 2.0客户端提供Amazon OAuth 2.0支持。

安装

要安装此包,请使用composer

composer require luchianenco/oauth2-amazon

用法

用法与The League的OAuth客户端相同,使用\Luchianenco\OAuth2\Client\Provider\Amazon作为提供者。

授权码流

$provider = new Luchianenco\OAuth2\Client\Provider\Amazon([
    'clientId'          => '{amazon-client-id}',
    'clientSecret'      => '{amazon-client-secret}',
    'redirectUri'       => 'https://example.com/callback_url',
]);

// Send OAuth Request
// If we don't have an authorization code then we can get one
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['OAuth2State'] = $provider->getState();

...

// OAuth2 Callback URL
// Compare given state against previously stored one to block CSRF attack
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['OAuth2State'])) {

    exit('Invalid state');
    
} else {

    // Try to get an access token
    $token = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);

    // Now we can look up users profile
    try {
        // Get the user's details
        $user = $provider->getResourceOwner($token);

        printf('Hello %s!', $user->getName());

    } catch (Exception $e) {
        // Failed to get user details
        exit('Oh no ... ...');
    }

    // We can use token to make other API calls
    echo $token->getToken();
}

测试

$ ./vendor/bin/phpunit