arm-yan / guzzle-oauth2
An OAuth2 plugin (subscriber) for Guzzle
2.2.0
2020-10-02 08:11 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-11-09 23:06:48 UTC
README
Provides an OAuth2 plugin (subscriber) for Guzzle.
Version 2.x (on the master
branch) is intended for Guzzle 5:
"arm-yan/guzzle-oauth2": "~2.0"
Guzzle 3 compatibility continues in the 1.0
branch:
"arm-yan/guzzle-oauth2": "~1.0"
Features
- Acquires access tokens via one of the supported grant types (code, client credentials, user credentials, refresh token). Or you can set an access token yourself.
- Supports refresh tokens (stores them and uses them to get new access tokens).
- Handles token expiration (acquires new tokens and retries failed requests).
Running the tests
First make sure you have all the dependencies in place by running composer install --prefer-dist
, then simply run ./bin/phpunit
.
Example
use GuzzleHttp\Client; use ArmYan\Guzzle\Oauth2\GrantType\RefreshToken; use ArmYan\Guzzle\Oauth2\GrantType\PasswordCredentials; use ArmYan\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.