debiprasad / oauth2-avaza
为 PHP League OAuth2-Client 提供的 Avaza OAuth 2.0 客户端提供者
0.1.1
2018-03-22 13:40 UTC
Requires
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.0
This package is not auto-updated.
Last update: 2024-09-29 05:14:49 UTC
README
此软件包为 PHP League 的 OAuth 2.0 客户端 提供了 Avaza OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require debiprasad/oauth2-avaza
使用方法
使用方法与 The League 的 OAuth 客户端相同,使用 \League\OAuth2\Client\Provider\Avaza
作为提供者。
授权代码流
$provider = new League\OAuth2\Client\Provider\Avaza([ 'clientId' => '{avaza-client-id}', 'clientSecret' => '{avaza-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); // If we don't have an authorization code then get one if (!isset($_GET['code'])) { // Fetch the authorization URL from the provider; this returns the // urlAuthorize option and generates and applies any necessary parameters // (e.g. state). $authorizationUrl = $provider->getAuthorizationUrl(); // Get the state generated for you and store it to the session. $_SESSION['oauth2state'] = $provider->getState(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) { if (isset($_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); } exit('Invalid state'); } else { try { // Try to get an access token using the authorization code grant. $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // We have an access token, which we may use in authenticated // requests against the service provider's API. echo 'Access Token: ' . $token->getToken() . "<br>"; echo 'Refresh Token: ' . $token->getRefreshToken() . "<br>"; echo 'Expired in: ' . $token->getExpires() . "<br>"; echo 'Already expired? ' . ($token->hasExpired() ? 'expired' : 'not expired') . "<br>"; // Using the access token, we may look up details about the // resource owner. $user = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $user->getEmail()); // The provider provides a way to get an authenticated API request for // the service, using the access token; it returns an object conforming // to Psr\Http\Message\RequestInterface. $request = $provider->getAuthenticatedRequest( 'GET', 'https://api.avaza.com/api/Contact', $token ); // Use this to interact with an API on the users behalf echo $token->getToken(); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } }
管理作用域
在创建您的 Avaza 授权 URL 时,您可以指定您的应用程序可能授权的状态和作用域。
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => ['read','write'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果没有定义,提供者将使用内部默认值。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。