previewtechs/oauth2-client

Preview Technologies 官方 OAuth2 客户端

v2.4 2018-03-05 06:52 UTC

This package is auto-updated.

Last update: 2024-09-06 10:08:29 UTC


README

此软件包为 Preview Technologies Limited 提供了 PHP League 的 OAuth 2.0 客户端 的 OAuth 2.0 支持。

Latest Stable Version License Build Status Code Coverage Code Quality

安装

要安装,请使用 composer

composer require previewtechs/oauth2-client

用法

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

授权码流

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

有关此软件包的进一步用法,请参阅 核心软件包文档中的“授权码授权”

要获取完整的权限范围列表,请访问 可用的 OAuth2.0 权限范围列表

刷新令牌

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',   // The client secret assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);
    
    //Remove old and save new access token in your database
}

有关此软件包的进一步用法,请参阅 核心软件包文档中的“刷新令牌”

完整用法

<?php
require "vendor/autoload.php";

session_start();

$provider = new \Previewtechs\Oauth2\Client\Provider([
    'clientId' => '{previewtechs_client_id}',
    // The client ID assigned to you by Preview Technologies
    'clientSecret' => '{previewtechs_client_secret}',
    // The client password assigned to you by Preview Technologies
    'redirectUri' => '{your_redirect_url}'
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    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 {

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

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo $accessToken->getToken() . "\n";
        echo $accessToken->getRefreshToken() . "\n";
        echo $accessToken->getExpires() . "\n";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";

        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $provider->getResourceOwner($accessToken);

        var_export($resourceOwner->toArray());

        // We provides a way to get an authenticated API request for
        // the service, using the access token; it returns an object conforming
        // to Psr\Http\Message\RequestInterface.
        $request = $provider->getAuthenticatedRequest(
            'GET',
            'https://myaccount.previewtechs.com/api/v1/identity/user-info',
            $accessToken
        );

        $client = new \GuzzleHttp\Client();
        $result = $client->send($request);

        var_export(json_decode($result->getBody()->getContents(), true));

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

        // Failed to get the access token or user details.
        exit($e->getMessage());

    }

}

贡献

请参阅 CONTRIBUTING 页面 了解详细信息。

致谢

许可证

MIT 许可证 (MIT)

注意:受到 PHP League 第三方提供者库 的强烈启发