stevenmaguire / oauth2-elance
The PHP League OAuth2-Client 的 Elance OAuth 2.0 客户端提供者
2.0.0
2017-01-26 02:24 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 auto-updated.
Last update: 2024-09-14 10:42:45 UTC
README
此软件包为 PHP League 的 OAuth 2.0 客户端 提供了 Elance OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require stevenmaguire/oauth2-elance
使用方法
使用方法与 The League 的 OAuth 客户端相同,使用 \Stevenmaguire\OAuth2\Client\Provider\Elance
作为提供者。
授权码流
$provider = new Stevenmaguire\OAuth2\Client\Provider\Elance([ 'clientId' => '{elance-client-id}', 'clientSecret' => '{elance-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(); $_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'] ]); // 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 $user = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $user->getId()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
关于 Elance API 的重要说明
此软件包尝试使用 /profiles/my API 端点来填充 ElanceResourceOwner
对象。如果资源所有者在 Elance 上有“承包商资料”,则此尝试将成功。如果资源所有者没有“承包商资料”,则请求此信息将导致 404 响应,上述代码示例将失败。
无论“承包商资料”状态如何,对 getAccessToken
的请求都将成功。
刷新令牌
一旦您的应用程序获得授权,您可以使用刷新令牌而不是整个获取全新令牌的过程来刷新已过期的令牌。为此,只需从您的数据存储中重新使用此刷新令牌来请求刷新即可。
$grant = new \League\OAuth2\Client\Grant\RefreshToken(); $token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);
测试
$ ./vendor/bin/phpunit
贡献
请参阅 CONTRIBUTING 获取详细信息。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 获取更多信息。