rampmaster/guzzle-oauth2-plugin

此软件包已被弃用且不再维护。未建议替代软件包。

为Guzzle提供的OAuth2插件(订阅者)

v2.1.2 2016-05-17 18:25 UTC

This package is auto-updated.

Last update: 2022-02-01 12:58:24 UTC


README

Guzzle提供OAuth2插件(订阅者)。

Build Status Code Coverage

版本2.x(在master分支上)旨在用于Guzzle 5

        "commerceguys/guzzle-oauth2-plugin": "~2.0"

Guzzle 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.