oakhope/oauth2-wechat

微信登录认证授权 Wechat login authorization. 本软件包为 PHP League 的 OAuth 2.0 Client 提供微信 OAuth 2.0 支持

v1.0.4 2017-09-15 10:44 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:55:43 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

本软件包为 PHP League 的 OAuth 2.0 Client 提供微信 OAuth 2.0 支持。

  • 完成

    网站 SDK、小程序

  • 待办事项

    移动应用 SDK

安装

要安装,请使用 composer

composer require oakhope/oauth2-wechat

用法

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

授权码流

$provider = new \Oakhope\OAuth2\Client\Provider\WebProvider([
        'appid' => '{wechat-client-id}',
        'secret' => '{wechat-client-secret}',
        'redirect_uri' => 'https://example.com/callback-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'] !== rtrim($_SESSION['oauth2state'], '#wechat_redirect'))) {

    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 "token: ".$accessToken->getToken()."<br/>";
        echo "refreshToken: ".$accessToken->getRefreshToken()."<br/>";
        echo "Expires: ".$accessToken->getExpires()."<br/>";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired')."<br/><br/>";

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

        var_export($resourceOwner->toArray());
        
    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

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

刷新令牌

一旦您的应用程序获得授权,您可以使用刷新令牌来刷新过期的令牌,而不是重新获取全新的令牌。为此,只需从您的数据存储中重用此刷新令牌来请求刷新。

此示例使用 Brent Shaffer 的演示 OAuth 2.0 应用程序 Lock'd In。有关授权码示例的更多详细信息,请参阅上面的示例。

$provider = new \Oakhope\OAuth2\Client\Provider\WebProvider([
        'appid' => '{wechat-client-id}',
        'secret' => '{wechat-client-secret}',
        'redirect_uri' => '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 --colors tests

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可协议

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