bookingsync / oauth2-bookingsync-php
为 PHP League OAuth2-Client 提供的 BookingSync OAuth 2.0 客户端提供者
0.1.3
2015-10-27 21:41 UTC
Requires
- php: >=5.4.0
- league/oauth2-client: ~0.12.1
Requires (Dev)
- jakub-onderka/php-parallel-lint: 0.8.*
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.0
This package is not auto-updated.
Last update: 2024-09-28 17:28:10 UTC
README
本软件包为 PHP League 的 OAuth 2.0 客户端提供了 BookingSync OAuth 2.0 支持。OAuth 2.0 Client。
弃用警告
此库使用已弃用的供应商。您可能遇到 SSL 证书问题。要解决此问题,您应该
- 将已弃用的 GuzzleHttp 仓库中的 cacert.pem 文件替换为从 https://curl.haxx.se/docs/caextract.html 更新的一个。
- 旧 pem 文件的存储位置是
vendor/guzzle/guzzle/src/Guzzle/Http/Resources/cacert.pem
安装
要安装,请使用 composer
composer require bookingsync/oauth2-bookingsync-php
用法
用法与 The League 的 OAuth 客户端相同,使用 \Bookingsync\OAuth2\Client\Provider\Bookingsync
作为提供者。
授权码流
$provider = new Bookingsync\OAuth2\Client\Provider\Bookingsync([ 'clientId' => 'XXXXXXXX', 'clientSecret' => 'XXXXXXXX', 'redirectUri' => 'https://www.example.com/callback-url', // https is mandatory for BookingSync 'scopes' => ['public'] // scopes required by your BookingSync application. ]); if (!isset($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->state; header('Location: '.$authUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Optional: Now you have a token you can look up a users profile data try { // We got an access token, let's now get the user's details $userDetails = $provider->getUserDetails($token); // Use these details to create a new profile printf('Hello %s!', $userDetails->screenName); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } // Use this to interact with an API on the users behalf echo $token->accessToken; // Use this to get a new access token if the old one expires echo $token->refreshToken; // Unix timestamp of when the token will expire, and need refreshing echo $token->expires; }
刷新令牌
$provider = new Bookingsync\OAuth2\Client\Provider\Bookingsync([ 'clientId' => '{bookingsync-client-id}', 'clientSecret' => '{bookingsync-client-secret}', 'redirectUri' => 'https://example.com/callback-url' ]); $grant = new \League\OAuth2\Client\Grant\RefreshToken(); $token = $provider->getAccessToken($grant, ['refresh_token' => $token->refreshToken]);
测试
phpunit test
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。