alofoxx / oauth2-gumroad
The PHP League OAuth2-Client 的 Gumroad OAuth 2.0 客户端提供者
v1.0.1
2021-02-16 19:28 UTC
Requires
- php: >=7.2
- league/oauth2-client: ^2.2.1
Requires (Dev)
- mockery/mockery: ~1.3.0
- php-parallel-lint/php-parallel-lint: ~0.9
- phpunit/phpunit: ~8.0
- squizlabs/php_codesniffer: ^2.0
This package is auto-updated.
Last update: 2024-09-17 03:26:44 UTC
README
此包为 PHP League 的 OAuth 2.0 客户端 v2.0 及以上版本提供了 Gumroad OAuth 2.0 支持。
要求
以下版本的 PHP 受支持。
- PHP 7.1
- PHP 7.2
- PHP 7.3
- PHP 7.4
安装
要安装,请使用 composer
$ composer require alofoxx/oauth2-gumroad
用法
用法与 The League 的 OAuth 客户端相同,使用 \Alofoxx\OAuth2\Client\Provider\Gumroad
作为提供者。
示例授权代码流程
以下自包含示例
- 获取授权代码
- 使用提供的授权代码获取访问令牌
- 使用提供的访问令牌查找用户的个人资料
您可以通过注册 Gumroad 应用并使用您的服务器上此示例脚本的重定向 URI 来尝试此脚本。然后,将 Gumroad 应用的应用 ID 和密钥以及相同的 URI 放入脚本顶部的设置中。
<?php require __DIR__ . '/vendor/autoload.php'; session_start(); echo ('Main screen turn on!<br/><br/>'); $provider = new \Alofoxx\OAuth2\Client\Provider\Gumroad([ 'clientId' => '{gumroad-application-id}', 'clientSecret' => '{gumroad-application-secret}', 'redirectUri' => '{your-server-uri-to-this-script-here}' ]); if (!isset($_GET['code'])) { // Step 1. Get authorization code $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); // 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 { // Step 2. Get an access token using the provided authorization code $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Show some token details echo '<h2>Token details:</h2>'; echo 'Token: ' . $token->getToken() . "<br/>"; echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>"; echo 'Expires: ' . $token->getExpires() . " - "; echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>"; // Step 3. (Optional) Look up the user's profile with the provided token try { $user = $provider->getResourceOwner($token); echo '<h2>Resource owner details:</h2>'; printf('Hello %s!<br/><br/>', $user->getName()); var_export($user->toArray()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } }
管理作用域
在步骤 1 中创建您的 Gumroad 授权 URL 时,您可以指定您的应用程序可以授权的状态和作用域。
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => ['edit_products', 'view_sales', 'mark_sales_as_shipped'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果两者都没有定义,提供者将使用内部默认的 view_sales
。
在编写此文档时,以下作用域可用
edit_products
view_sales
mark_sales_as_shipped
refund_sales
刷新令牌
您可以使用刷新令牌而不是通过整个获取全新令牌的过程来刷新过期的令牌。要这样做,只需从您的数据存储中重复使用新的令牌来请求刷新即可。
// create $provider as in the initial example $existingAccessToken = getAccessTokenFromYourDataStore(); if ($existingAccessToken->hasExpired()) { $newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]); // Purge old access token and store new access token to your data store. }
测试
$ ./vendor/bin/parallel-lint src test
$ ./vendor/bin/phpcs src --standard=psr2 -sp
$ ./vendor/bin/phpunit --coverage-text
贡献
有关详细信息,请参阅贡献指南。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅许可文件。