PHP code example of benjaminfavre / oauth2-http-client
1. Go to this page and download the library: Download benjaminfavre/oauth2-http-client 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/ */
benjaminfavre / oauth2-http-client example snippets
use Symfony\Component\HttpClient\HttpClient;
use BenjaminFavre\OAuthHttpClient\OAuthHttpClient;
use BenjaminFavre\OAuthHttpClient\GrantType\ClientCredentialsGrantType;
$httpClient = HttpClient::create();
// Here we will use the client credentials grant type but it could be any other grant type
$grantType = new ClientCredentialsGrantType(
$httpClient,
'https://github.com/login/oauth/access_token', // The OAuth server token URL
'the-client-id',
'the-client-password'
);
$httpClient = new OAuthHttpClient($httpClient, $grantType);
// Then use $httpClient to make your API calls:
// $httpClient->request(...);
declare(strict_types=1);
namespace App\Sharepoint;
use BenjaminFavre\OAuthHttpClient\GrantType\GrantTypeInterface;
use BenjaminFavre\OAuthHttpClient\GrantType\Tokens;
use BenjaminFavre\OAuthHttpClient\GrantType\TokensExtractor;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final class CustomClientCredentialsGrantType implements GrantTypeInterface
{
use TokensExtractor;
public function __construct(
private HttpClientInterface $client,
private string $sharepointOauthClientId,
private string $sharepointOauthClientSecret,
private string $sharepointOauthUrl,
private string $sharepointOauthResource,
) {
}
public function getTokens(): Tokens
{
$response = $this->client->request(Request::METHOD_POST, $this->sharepointOauthUrl, [
'body' => http_build_query([
'grant_type' => 'client_credentials',
'client_id' => $this->sharepointOauthClientId,
'client_secret' => $this->sharepointOauthClientSecret,
'resource' => $this->sharepointOauthResource,
]),
]);
return $this->extractTokens($response);
}
}
public function __construct(
private OAuthHttpClient $sharepointClient,
) {
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.