growthoptimized/oauth2-optimizely

该软件包已被废弃,不再维护。作者建议使用 widerfunnel/oauth2-optimizely 软件包。

Optimizely OAuth 2.0 客户端提供者,用于 PHP League OAuth2-Client

3.0.1 2017-10-15 01:42 UTC

This package is not auto-updated.

Last update: 2019-02-20 19:08:42 UTC


README

Packagist Travis

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

安装

要安装,请使用 composer

composer require widerfunnel/oauth2-optimizely

使用

使用方式与 The League 的 OAuth 客户端相同,使用 \WiderFunnel\OAuth2\Client\Provider\Optimizely 作为提供者。

授权码流程

注意:目前 Optimizely 不允许通过其 API 访问资源所有者配置文件。如果 Optimizely API 更新,将相应更新。

$provider = new WiderFunnel\OAuth2\Client\Provider\Optimizely([
    'clientId'          => '{optimizely-client-id}',
    'clientSecret'      => '{optimizely-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

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

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the projects details
        $project = $provider->getResourceOwner($token);        

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}

刷新令牌

$provider = new WiderFunnel\OAuth2\Client\Provider\Optimizely([
    'clientId'          => '{optimizely-client-id}',
    'clientSecret'      => '{optimizely-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);

    // Purge old access token and store new access token to your data store.
}

测试

$ ./vendor/bin/phpunit

贡献

请参阅 CONTRIBUTING 获取详细信息。

致谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。