calcinai / oauth2-xero
为 PHP League OAuth2-Client 提供的 Xero OAuth 2.0 客户端提供程序
v1.3.0
2022-10-10 20:42 UTC
Requires
- php: >=7.1.0
- firebase/php-jwt: ^5.0|^6.0
- league/oauth2-client: 1.*|2.*
README
此包为 PHP League 的 OAuth 2.0 客户端 提供Xero OAuth 2.0 支持。
安装
要安装,请使用 composer
$ composer require calcinai/oauth2-xero
用法
用法与 The League 的 OAuth 客户端相同,使用 \Calcinai\OAuth2\Client\Provider\Xero
作为提供程序。
授权码流
session_start(); $provider = new \Calcinai\OAuth2\Client\Provider\Xero([ 'clientId' => '{xero-client-id}', 'clientSecret' => '{xero-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); if (!isset($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl([ 'scope' => 'openid email profile accounting.transactions' ]); $_SESSION['oauth2state'] = $provider->getState(); 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'] ]); //If you added the openid/profile scopes you can access the authorizing user's identity. $identity = $provider->getResourceOwner($token); print_r($identity); //Get the tenants that this user is authorized to access $tenants = $provider->getTenants($token); print_r($tenants); }
然后您可以存储令牌并使用它来对 API 发出请求,以访问所需的租户。
作用域
OAuth 作用域,表示您希望您的应用程序能够访问 Xero 组织的哪些部分。完整的作用域列表可以在 这里 找到。
$provider = new \Calcinai\OAuth2\Client\Provider\Xero([ 'clientId' => '{xero-client-id}', 'clientSecret' => '{xero-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); $authUrl = $provider->$provider->getAuthorizationUrl([ 'scope' => 'bankfeeds accounting.transactions' ]);
刷新令牌
$newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]);
测试
$ ./vendor/bin/phpunit
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。