PHP code example of kamermans / guzzle-oauth2-subscriber
1. Go to this page and download the library: Download kamermans/guzzle-oauth2-subscriber 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/ */
kamermans / guzzle-oauth2-subscriber example snippets
$oauth = new OAuth2Subscriber($grant_type);
$client = new Client([
'auth' => 'oauth',
]);
$client->getEmitter()->attach($oauth);
$oauth = new OAuth2Middleware($grant_type);
$stack = HandlerStack::create();
$stack->push($oauth);
$client = new Client([
'auth' => 'oauth',
'handler' => $stack,
]);
$oauth = new OAuth2Middleware($grant_type);
$client->getConfig('handler')->push($oauth);
use kamermans\OAuth2\GrantType\ClientCredentials;
use kamermans\OAuth2\OAuth2Subscriber;
// Authorization client - this is used to request OAuth access tokens
$reauth_client = new GuzzleHttp\Client([
// URL for access_token request
'base_url' => 'http://some_host/access_token_request_url',
]);
$reauth_config = [
"client_id" => "your client id",
"client_secret" => "your client secret",
"scope" => "your scope(s)", // optional
"state" => time(), // optional
];
$grant_type = new ClientCredentials($reauth_client, $reauth_config);
$oauth = new OAuth2Subscriber($grant_type);
// This is the normal Guzzle client that you use in your application
$client = new GuzzleHttp\Client([
'auth' => 'oauth',
]);
$client->getEmitter()->attach($oauth);
$response = $client->get('http://somehost/some_secure_url');
echo "Status: ".$response->getStatusCode()."\n";
use kamermans\OAuth2\GrantType\ClientCredentials;
use kamermans\OAuth2\OAuth2Middleware;
use GuzzleHttp\HandlerStack;
// Authorization client - this is used to request OAuth access tokens
$reauth_client = new GuzzleHttp\Client([
// URL for access_token request
'base_uri' => 'http://some_host/access_token_request_url',
]);
$reauth_config = [
"client_id" => "your client id",
"client_secret" => "your client secret",
"scope" => "your scope(s)", // optional
"state" => time(), // optional
];
$grant_type = new ClientCredentials($reauth_client, $reauth_config);
$oauth = new OAuth2Middleware($grant_type);
$stack = HandlerStack::create();
$stack->push($oauth);
// This is the normal Guzzle client that you use in your application
$client = new GuzzleHttp\Client([
'handler' => $stack,
'auth' => 'oauth',
]);
$response = $client->get('http://somehost/some_secure_url');
echo "Status: ".$response->getStatusCode()."\n";
use kamermans\OAuth2\Signer\ClientCredentials\SignerInterface;
class MyCustomAuth implements SignerInterface
{
public function sign($request, $clientId, $clientSecret)
{
if (Helper::guzzleIs('~', 6)) {
$request = $request->withHeader('x-client-id', $clientId);
$request = $request->withHeader('x-client-secret', $clientSecret);
return $request;
}
$request->setHeader('x-client-id', $clientId);
$request->setHeader('x-client-secret', $clientSecret);
return $request;
}
}
use kamermans\OAuth2\Persistence\FileTokenPersistence;
$token_path = '/tmp/access_token.json';
$token_persistence = new FileTokenPersistence($token_path);
$grant_type = new ClientCredentials($reauth_client, $reauth_config);
$oauth = new OAuth2Middleware($grant_type);
$oauth->setTokenPersistence($token_persistence);
// We'll store everything in an array, but you can use any provider you want
$cache = [];
$cache_key = "foo";
// Returns true if the item exists in cache
$exists = function() use (&$cache, $cache_key) {
return array_key_exists($cache_key, $cache);
};
// Sets the given $value array in cache
$set = function(array $value) use (&$cache, $cache_key) {
$cache[$cache_key] = $value;
};
// Gets the previously-stored value from cache (or null)
$get = function() use (&$cache, $cache_key, $exists) {
return $exists()? $cache[$cache_key]: null;
};
// Deletes the previously-stored value from cache (if exists)
$delete = function() use (&$cache, $cache_key, $exists) {
if ($exists()) {
unset($cache[$cache_key]);
}
};
$persistence = new ClosureTokenPersistence($set, $get, $delete, $exists);
use kamermans\OAuth2\GrantType\NullGrantType;
$oauth = new OAuth2Middleware(new NullGrantType);
$oauth->setAccessToken([
// Your access token goes here
'access_token' => 'abcdefghijklmnop',
// You can specify 'expires_in` as well, but it doesn't make much sense in this scenario
// You can also specify 'scope' => 'list of scopes'
]);
// This grant type is used to get a new Access Token and Refresh Token when
// no valid Access Token or Refresh Token is available
$grant_type = new ClientCredentials($reauth_client, $reauth_config);
// This grant type is used to get a new Access Token and Refresh Token when
// only a valid Refresh Token is available
$refresh_grant_type = new RefreshToken($reauth_client, $reauth_config);
// Tell the middleware to use the two grant types
$oauth = new OAuth2Middleware($grant_type, $refresh_grant_type);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.