indibeast/oauth2-human

Human OAuth 2.0 support for the PHP League's OAuth 2.0 Client

dev-master 2016-02-09 10:22 UTC

This package is not auto-updated.

Last update: 2024-04-17 05:46:57 UTC


README

Build Status

This package provides Human OAuth 2.0 support for the PHP League's OAuth 2.0 Client.

Usage

Authorization Code Flow

$provider = new \Human\OAuth2\Client\Provider\Human([
    'clientId'      => 'XXXXXXXX',
    'clientSecret'  => 'XXXXXXXX',
    'redirectUri'   => 'https://your-registered-redirect-uri/',
    'account'       => 'XXXXX'

]);

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

    $authurl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

 } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])){

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

 } else {

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

     try {

        $client = new Client($token);
        $user   = $client->getResourceOwner();
        echo $user->getFirstName();

     } catch (Exception $e) {

        echo "Oops!..."
     }

 }

License

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