The OAuthHelper class provides methods for all OAuth 2.0 flows. Access it via $client->oauth().
Methods #
// Generate an authorization URL for the Authorization Code flow
$client->oauth()->getAuthorizationUrl(
string $clientId,
string $redirectUri,
string $scope,
string $state,
string $codeChallenge,
string $codeChallengeMethod = 'S256'
): string
// Exchange an authorization code for tokens
$client->oauth()->exchangeCode(
string $clientId,
string $clientSecret,
string $code,
string $redirectUri,
string $codeVerifier
): array
// Refresh an access token using a refresh token
$client->oauth()->refreshToken(
string $clientId,
string $clientSecret,
string $refreshToken
): array
// Obtain an access token using Client Credentials grant
$client->oauth()->clientCredentials(
string $clientId,
string $clientSecret,
string $scope = ''
): array
// Revoke an access or refresh token
$client->oauth()->revokeToken(
string $clientId,
string $clientSecret,
string $token
): array
Authorization Code Flow Example #
// Step 1: Generate PKCE values
$codeVerifier = bin2hex(random_bytes(32));
$codeChallenge = rtrim(strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '=');
// Step 2: Redirect user to authorization URL
$authUrl = $client->oauth()->getAuthorizationUrl(
clientId: 'my-client-id',
redirectUri: 'https://myapp.com/oauth/callback',
scope: 'licenses:read customers:read',
state: bin2hex(random_bytes(16)),
codeChallenge: $codeChallenge,
codeChallengeMethod: 'S256'
);
header('Location: ' . $authUrl);
// Step 3: After redirect, exchange the code
$tokens = $client->oauth()->exchangeCode(
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
code: $_GET['code'],
redirectUri: 'https://myapp.com/oauth/callback',
codeVerifier: $codeVerifier
);
$accessToken = $tokens['access_token'];
$refreshToken = $tokens['refresh_token'];
Client Credentials Flow Example #
$tokens = $client->oauth()->clientCredentials(
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
scope: 'licenses:read products:read'
);
$accessToken = $tokens['access_token'];
