worksection / oauth2-worksection
Worksection OAuth 2.0 support for the PHP League's OAuth 2.0 Client
Installs: 3 502
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
- league/oauth2-client: ^2.0
This package is not auto-updated.
Last update: 2024-11-07 16:43:55 UTC
README
This package provides Worksection OAuth 2.0 support for the PHP League's OAuth 2.0 Client. Worksection FAQ about OAuth2.0
Installation
composer require worksection/oauth2-worksection
Usage
use Worksection\OAuth2\Client\Provider\Worksection as WorksectionOauth2; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; $provider = new WorksectionOauth2([ 'clientId' => 'yourId', 'clientSecret' => 'yourSecret', 'redirectUri' => 'https://redirecturl.com/query' ]); if (!$_REQUEST['code']) { $authorizationUrl = $provider->getAuthorizationUrl(['scope' => 'projects_read users_write']); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authorizationUrl); } elseif ($_REQUEST['state']) { if ($_REQUEST['state'] !== $_SESSION['oauth2state']) { exit('Invalid state'); } else { unset($_SESSION['oauth2state']); } // Access token try { $accessToken = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]); } catch (IdentityProviderException $e) { exit($e->getMessage()); } // Resource info try { $resourceOwner = $provider->getResourceOwner($accessToken); } catch (IdentityProviderException $e) { exit($e->getMessage()); } //echo 'Access Token: ' . $accessToken->getToken(); //echo 'Refresh Token: ' . $accessToken->getRefreshToken(); //echo 'Account URL: ' . $accessToken->getValues()['account_url']; //echo 'Expired in: ' . $accessToken->getExpires(); //echo 'Resource Owners ID: ' . $resourceOwner->getId(); //echo 'Resource Owners NAME: ' . $resourceOwner->getName(); //echo 'Resource Owners EMAIL: ' . $resourceOwner->getEmail(); //echo 'Resource Owners Account URL: ' . $resourceOwner->getAccountUrl(); // Make some API request using Access Token $options = [ 'body' => json_encode([ 'action' => 'get_tasks', 'page' => '/project/193/' ]), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $request = $provider->getAuthenticatedRequest('POST', $resourceOwner->getAccountUrl() . '/api/oauth2', $accessToken, $options); try { $response = $provider->getParsedResponse($request); } catch (IdentityProviderException $e) { exit($e->getMessage()); } var_dump($response); }
For more information see the PHP League's general usage examples.
License
The MIT License (MIT).