league/oauth2-client
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
guzzlehttp/guzzle | 7.8.1 | 112.34 kB | MIT | prod | |
paragonie/random_compat | v9.99.100 | 5.93 kB | MIT | prod |
The league/oauth2-client package offers a platform for smooth integration with various OAuth 2.0 service providers. As a robust OAuth 2.0 client library, this package can interact seamlessly with any OAuth 2.0 provider that sticks to the OAuth 2.0 Authorization Framework. Notably, it comes with a 'GenericProvider' class for hassle-free connection with any service provider leveraging Bearer tokens. The library also encourages customization, allowing you to extend and modify its operations to suit additional functionalities offered by various service providers beyond the standard OAuth 2.0 requirements.
To effectively use the league/oauth2-client, you will first need to install it via composer with the command composer require league/oauth2-client
. Once installed, you can now integrate various OAuth 2.0 providers. Here's a basic usage example with a GenericProvider
:
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => 'your-client-id', // The client ID assigned by the provider
'clientSecret' => 'your-client-secret', // The client password assigned by the provider
'redirectUri' => 'your-redirect-uri',
'urlAuthorize' => 'provider-url-authorize',
'urlAccessToken' => 'provider-url-access-token',
'urlResourceOwnerDetails' => 'provider-url-resource-owner-details',
]);
// Get authorization code
if (!isset($_GET['code'])) {
// Options to pass to `getAuthorizationUrl` method
$options = [
'scope' => ['email'] // Array of scopes you want to get
];
// Fetch the authorization URL from the provider; this returns the urlAuthorize option and generates and applies any necessary parameters (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl($options);
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
// Get access token
} else {
try {
// Get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token
exit($e->getMessage());
}
}
Remember to replace 'your-client-id'
, 'your-client-secret'
, 'your-redirect-uri'
, 'provider-url-authorize'
, 'provider-url-access-token'
, and 'provider-url-resource-owner-details'
with appropriate details relating to the OAuth service provider you are working with.
The official documentation for the league/oauth2-client is available at the following location: OAuth 2.0 Client Documentation. This comprehensive guide offers detailed explanations on usage, lists of available official and third-party PHP League provider clients, and instructions on how to implement your own provider client. The guide also provides various practical examples demonstrating how to perform different operations using the library.