frankkessler/guzzle-oauth2-middleware

适用于 Guzzle 的 OAuth2 中间件 - 基于 Commerceguys/guzzle-oauth2-plugin 包构建

v0.1.1 2016-10-05 04:33 UTC

README

改编自 Commerceguys/guzzle-oauth2-plugin 为 Guzzle 6+ 版本提供 OAuth2 中间件。

Build Status Coverage Status StyleCI Latest Stable Version

功能

  • 通过支持的授权类型之一(代码、客户端凭证、用户凭证、刷新令牌)获取访问令牌。或者您可以自己设置访问令牌。
  • 支持刷新令牌(存储它们并使用它们获取新的访问令牌)。
  • 处理令牌过期(获取新的令牌并重试失败的请求)。

运行测试

在 Windows 上,您必须将随 PHP 版本提供的 openssl.cnf 文件放置在以下位置

C:\usr\local\ssl\openssl.cnf

首先,通过运行 composer install --prefer-dist 确保所有依赖项都已正确安装。您还需要安装 node 来运行测试。您可以简单地运行 make test 以启动 node 服务器,运行测试,然后关闭 node 服务器。

或者,如果您想以调试模式运行 node 服务器,可以运行 node tests/server.js 8126 true,然后运行 vendor/bin/phpunit 以运行测试。

示例

use GuzzleHttp\Client;
use Frankkessler\Guzzle\Oauth2\GrantType\RefreshToken;
use Frankkessler\Guzzle\Oauth2\GrantType\PasswordCredentials;
use Frankkessler\Guzzle\Oauth2\Oauth2Client;

$base_uri = 'https://example.com';

$client = new Oauth2Client(['base_uri' => $base_uri]);

$config = [
    'username' => 'test@example.com',
    'password' => 'test password',
    'client_id' => 'test-client',
    'scope' => 'administration',
];

$token = new PasswordCredentials($config);
$client->setGrantType($token);

$refreshToken = new RefreshToken($config);
$client->setRefreshTokenGrantType($refreshToken);

$response = $client->get('https://example.com/api/user/me');

$response_headers = $response->getHeaders();

$response_code = $response->getStatusCode();

$response_body = (string) $response->getBody();

// Use $client->getAccessToken(); and $client->getRefreshToken() to get tokens
// that can be persisted for subsequent requests.