hernandev/oauth2-sc2
SteemConnect V2 Oauth 客户端提供商。
Requires
- php: >=7.1.0
- ext-curl: *
- ext-json: *
- guzzlehttp/guzzle: ^6.3
- illuminate/support: ^5.6
- league/oauth2-client: ^2.3
- symfony/http-foundation: ^4.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-08-25 04:14:47 UTC
README
这个库是一个简单易用的 SteemConnect V2 客户端的 OAuth2 客户端。
它简化了集成和解析所有具体细节的过程,为那些希望通过 SteemConnect V2 处理授权的人提供了一个极佳的授权流程。
0. 为什么?
OAuth2 并不复杂,这个项目是在构建 PHP 的 SteemConnect V2 SDK 时开始的,所以这个项目是即将在接下来的几天中出现的另一个软件包的授权部分。
1. 安装。
你所需要做的只是通过 composer 将这个库作为你的项目的依赖项安装
composer require hernandev/oauth2-sc2
2. 使用
在使用这个库之前,请记住你需要一个 SteemConnect 应用客户端 ID 和密钥。
2.1. 配置
这再简单不过了。只需创建一个配置实例,传入你的应用程序凭据
use SteemConnect\OAuth2\Config\Config; // creates the configuration object: $config = new Config('your.app', 'your-long-secret-id-here-keep-it-secret');
设置你的凭据后,你需要决定要请求哪些权限的权限
// set the scopes you want. $config->setScopes([ 'login', 'vote', 'comment', 'comment_delete' ]);
如果你不确定需要哪些权限,这些权限在 SteemConnect wiki 上有文档说明。
最后,我们将配置用户在授权后应该返回的 URL
// set the return / callback URL. $config->setReturnUrl('https://my-steem-app-is-awesome.com/login/return');
2.2. 重定向到授权
这就是你需要配置的所有内容来使用这个库,很酷吧?
现在,当然,你需要将用户重定向到 SteemConnect,在那里他们将授权你代表他们行事。
use SteemConnect\OAuth2\Provider\Provider; // first, we create a provider instance, passing the configuration needed: $provider = new Provider($config);
// 最后,我们可以获取重定向 URL,因此我们可以将用户发送到 SteemConnect
// get the URL string that you will redirect to. $provider->getAuthorizationUrl()
2.3. 解析返回值
猜猜看?超级难做
你将需要之前使用的配置和提供者代码,所以我假设你很聪明,会把这种逻辑放在一个公共的地方。
// just call the parse return URL and this library will automatically exchange the access code by the actual token: $token = $provider->parseReturn();
在上一次调用中,$token 是 AccessToken
类的实例,它可以用于以下操作
// gets the actual token that will be used on requests. $token->getToken(); // gets the expiration date, so you know the token is no longer valid. $token->getExpires(); // gets the refresh token, which may be used to issue a new token, if the offline scope was requested. $token->getRefreshToken(); // gets the standard ID for the account authorizing your app, it means, this field retrieves the account @username $token->getResourceOwnerId();
2.4. 存储令牌。
在获取到某个用户的访问令牌后,在大多数情况下,你需要存储令牌以供以后使用。
无论存储令牌的方法如何(浏览器会话/数据库),你都需要序列化令牌,然后稍后再解码。
这可以通过提供者上的两个方法轻松完成
编码
// encode the AccessToken instance into a JSON string. $tokenJson = $provider->encodeToken($token);
解码
// decode the JSON string token back into a AccessToken instance. $token = $provider->decodeToken($tokenJson);
2.5. 刷新过期的令牌。
当使用 offline
范围发布令牌时,生成的 AccessToken
实例将包含一个名为 `refresh_token` 的字段。刷新令牌可以用于发布新的令牌。
要使用包含刷新令牌的现有令牌发布新令牌,只需这样做
// get a new token from the existing / expired one. $newToken = $provider->refreshToken($token);
或者
如果你只存储了 `refresh_token` 字段值,你可以直接使用该字符串,通过这样做
// get a new AccessToken using the refresh token string. $token = $provider->refreshTokenString($refreshTokenString);
2.5. 额外内容
当然还有额外内容!
这个库实现了 ResourceOwner
接口,这意味着你也可以立即查询授权你权限的账户的一些信息
// gets the resource owner instance. $owner = $provider->getResourceOwner($token); // now you can use any key you may see on steemd.com directly on that $owner object!!! $owner->profile_json; $owner->balance; $owner->reputation; // and so on...
这就是全部内容!