datingvip/oauth2-client

OAuth 2.0 客户端库

v0.9 2020-07-01 15:32 UTC

This package is auto-updated.

Last update: 2024-08-29 03:32:18 UTC


README

这个库使得将您的应用程序与OAuth 2.0 身份提供者集成变得非常简单。它内置了对以下提供者的支持:

  • Facebook
  • Github
  • Google
  • Instagram
  • LinkedIn
  • Microsoft

添加对其他提供者的支持非常简单。

该库需要PHP 5.3+,并且与PSR-0兼容。

用法

$provider = new League\OAuth2\Client\Provider\<provider name>(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  'http://your-registered-redirect-uri/'
));

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

	// If we don't have an authorization code then get one
    $provider->authorize();

} else {

    try {

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

        try {

        	// We got an access token, let's now get the user's details
            $userDetails = $provider->getUserDetails($t);

            foreach ($userDetails as $attribute => $value) {
                var_dump($attribute, $value) . PHP_EOL . PHP_EOL;
            }

        } catch (Exception $e) {

            // Failed to get user details

        }

    } catch (Exception $e) {

        // Failed to get access token

    }
}

内置身份提供者列表

注意:返回URL的提供者有时会包含用户提供的附加URL。这些URL已经被括号[]括起来。