2017 © Pedro Peláez
 

library oauth2-vkontakte

Vkontakte provider for league/oauth2-client

image

max107/oauth2-vkontakte

Vkontakte provider for league/oauth2-client

  • Sunday, April 1, 2018
  • by max107
  • Repository
  • 0 Watchers
  • 0 Stars
  • 227 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 44 % Grown

The README.md

Vkontakte OAuth2 client provider

Build Status Latest Stable Version License, (*1)

This package provides Vkontakte integration for OAuth2 Client by the League., (*2)

Installation

composer require j4k/oauth2-vkontakte

Configuration

$provider = new J4k\OAuth2\Client\Provider\Vkontakte([
    'clientId'     => '1234567',
    'clientSecret' => 's0meRe4lLySEcRetC0De',
    'redirectUri'  => 'https://example.org/oauth-endpoint',
    'scopes'       => ['email', 'offline', 'friends'],
]);

Authorization

// Authorize if needed
if (PHP_SESSION_NONE === session_status()) session_start();
$isSessionActive = PHP_SESSION_ACTIVE === session_status();
$code            = !empty($_GET['code'])  ? $_GET['code']  : null;
$state           = !empty($_GET['state']) ? $_GET['state'] : null;
$sessionState    = 'oauth2state';

// No code – get some
if (!$code) {
    $authUrl = $provider->getAuthorizationUrl();
    if ($isSessionActive) $_SESSION[$sessionState] = $provider->getState();
    // Redirect user to VK
    header("Location: $authUrl");
    die();
}

// Anti-CSRF
elseif ($isSessionActive && (empty($state) || ($state !== $_SESSION[$sessionState]))) {
    unset($_SESSION[$sessionState]);
    throw new \RuntimeException('Invalid state');
}

// Exchange code to access_token
else {
    try {
        $providerAccessToken = $provider->getAccessToken('authorization_code', ['code' => $code]);
        // Yay, got it!
        var_dump([
            'access_token'  => $providerAccessToken->getToken(),
            'expires'       => $providerAccessToken->getExpires(),
            'user_id'       => $providerAccessToken->getValues()['user_id'],
            'email'         => $providerAccessToken->getValues()['email'], // Only for "email" scope
        ]);
    }
    catch (IdentityProviderException $e) {
        // Log error
        error_log($e->getMessage());
    }
}

Helper methods

Public

$provider->usersGet([1234, 56789]); // => \J4k\OAuth2\Client\Provider\User[]
$provider->friendsGet(23456);        // => \J4k\OAuth2\Client\Provider\User[]

With additional data

$providerAccessToken = new \League\OAuth2\Client\Token\AccessToken(['access_token' => 'iAmAccessTokenString']);
$provider->usersGet([1234, 56789], $providerAccessToken); // => \J4k\OAuth2\Client\Provider\User[]
$provider->friendsGet(23456, $providerAccessToken);        // => \J4k\OAuth2\Client\Provider\User[]

Contributions

Contributions are very welcome. Please submit a PR, (*3)

The Versions

01/04 2018

dev-master

9999999-dev

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package

01/04 2018

1.0

1.0.0.0

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package

01/04 2018

2.0

2.0.0.0

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package

13/09 2016

1.1.1

1.1.1.0

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package

01/09 2016

v1.1.0

1.1.0.0

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package

28/03 2015

v1.0.3

1.0.3.0

Vkontakte provider for league/oauth2-client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jack Wall

league package