commerceguys / guzzle-oauth2-plugin
为 Guzzle 提供OAuth2插件(订阅者)
v2.1.1
2015-12-12 23:27 UTC
Requires
- firebase/php-jwt: ~2.0
- guzzlehttp/guzzle: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.5
This package is auto-updated.
Last update: 2024-08-25 05:52:53 UTC
README
为Guzzle提供OAuth2插件(订阅者)。
2.x版本(位于master
分支)是为Guzzle 5设计的
"commerceguys/guzzle-oauth2-plugin": "~2.0"
3的兼容性在1.0
分支中继续
"commerceguys/guzzle-oauth2-plugin": "~1.0"
特性
- 通过支持的授权类型(代码、客户端凭证、用户凭证、刷新令牌)之一获取访问令牌。或者您也可以自行设置访问令牌。
- 支持刷新令牌(存储它们并使用它们来获取新的访问令牌)。
- 处理令牌过期(获取新的令牌并重试失败的请求)。
运行测试
首先,通过运行 composer install --prefer-dist
确保所有依赖都已正确安装,然后只需运行 ./bin/phpunit
。
示例
use GuzzleHttp\Client; use CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken; use CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials; use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber; $base_url = 'https://example.com'; $oauth2Client = new Client(['base_url' => $base_url]); $config = [ 'username' => 'test@example.com', 'password' => 'test password', 'client_id' => 'test-client', 'scope' => 'administration', ]; $token = new PasswordCredentials($oauth2Client, $config); $refreshToken = new RefreshToken($oauth2Client, $config); $oauth2 = new Oauth2Subscriber($token, $refreshToken); $client = new Client([ 'defaults' => [ 'auth' => 'oauth2', 'subscribers' => [$oauth2], ], ]); $response = $client->get('https://example.com/api/user/me'); print_r($response->json()); // Use $oauth2->getAccessToken(); and $oauth2->getRefreshToken() to get tokens // that can be persisted for subsequent requests.