PHP code example of sainsburys / guzzle-oauth2-plugin
1. Go to this page and download the library: Download sainsburys/guzzle-oauth2-plugin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sainsburys / guzzle-oauth2-plugin example snippets
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshToken;
use Sainsburys\Guzzle\Oauth2\GrantType\PasswordCredentials;
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware;
$baseUri = 'https://example.com';
$config = [
PasswordCredentials::CONFIG_USERNAME => '[email protected]',
PasswordCredentials::CONFIG_PASSWORD => 'test password',
PasswordCredentials::CONFIG_CLIENT_ID => 'test-client',
PasswordCredentials::CONFIG_TOKEN_URL => '/oauth/token',
'scope' => 'administration',
];
$oauthClient = new Client(['base_uri' => $baseUri]);
$grantType = new PasswordCredentials($oauthClient, $config);
$refreshToken = new RefreshToken($oauthClient, $config);
$middleware = new OAuthMiddleware($oauthClient, $grantType, $refreshToken);
$handlerStack = HandlerStack::create();
$handlerStack->push($middleware->onBefore());
$handlerStack->push($middleware->onFailure(5));
$client = new Client(['handler'=> $handlerStack, 'base_uri' => $baseUri, 'auth' => 'oauth2']);
$response = $client->request('GET', '/api/user/me');
// Use $middleware->getAccessToken(); and $middleware->getRefreshToken() to get tokens
// that can be persisted for subsequent requests.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.