runalyze/php-oauth2-runalyze

Runalyze OAuth 2.0 客户端提供商,用于 PHP League 的 OAuth 2.0 客户端

dev-main 2023-03-21 16:19 UTC

This package is not auto-updated.

Last update: 2024-09-18 21:34:58 UTC


README

Latest Version Total Downloads

本软件包为 PHP League 的 OAuth 2.0 客户端提供 Runalyze OAuth 2.0 支持。OAuth 2.0 客户端

安装

要安装此软件包,请使用 composer

composer require runalyze/oauth2-runalyze

使用方法

使用方法与 The League 的 OAuth 客户端相同,使用 \Runalyze\OAuth2\Client\Provider\Runalyze 作为提供商。

授权码流程

$provider = new Runalyze\OAuth2\Client\Provider\Runalyze([
    'clientId'          => '{runalyze-client-id}',
    'clientSecret'      => '{runalyze-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