Skip to content

xiabin/oauth2-sinaweibo

Repository files navigation

SinaWeibo Provider for OAuth 2.0 Client

license Packagist Build Status Coverage Status Quality Score Packagist

这个组件基于 PHP League's OAuth 2.0 Client.

安装

用composer安装:

composer require xiabin/oauth2-sinaweibo

用例

获取token

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

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

    //如果没有code就去获取
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

  //判断state与之前使用的是否一致
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

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

} else {

    //获取accesstoken
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

    ...
}

测试

$ ./vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

About

Sina Weibo Provider for the OAuth 2.0 Client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages