$response = Http::withRefreshToken(
'https://example.com/token.oauth2',
[
'client_id',
'client_secret',
],
[
'scopes' => [],
'expires' => 'expires_in', // When token should be considered expired. A string key in the response JSON for the expiration. We try to parse different formats and then remove 1 minute to be on the safe side.
'auth_type' => 'body', // 'body' or 'header'
'access_token' => 'access_token', // Key for the access token in the response JSON
],
'Bearer'
)->get(
'https://example.com/api',
);
use Pelmered\LaravelHttpOAuthHelper\AccessToken;
use Pelmered\LaravelHttpOAuthHelper\Credentials;
use Pelmered\LaravelHttpOAuthHelper\Options;
use Pelmered\LaravelHttpOAuthHelper\RefreshToken;
$response = Http::withRefreshToken(
'https://example.com/token.oauth2',
new Credentials(
clientId: 'client_id',
clientSecret: 'client_secret',
),
new Options(
scopes: ['scope1', 'scope2'],
expires: 3600,
grantType: 'password_credentials',
authType: Credentials::AUTH_TYPE_BODY,
tokenType: AccessToken::TOKEN_TYPE_BEARER,
),
)->get(
'https://example.com/api',
);
$response = Http::withRefreshToken(
'https://example.com/token.oauth2',
[
'client_id',
'client_secret',
],
[
'expires' => fn($response) => $response->json()['expires_in'] - 300, // Should return the ttl in seconds that has been parsed from the response and can be manipulated as you want.
'access_token' => fn($response) => $response->access_token, // Should return the access token that has been parsed from the response.
],
'Bearer'
)->get(
'https://example.com/api',
);
use Illuminate\Http\Client\PendingRequest;
$response = Http::withRefreshToken(
'https://example.com/token.oauth2',
[
'client_id',
'client_secret',
],
[
'expires' => fn($response) => $response->json()['expires_in'] - 300, // Should return the ttl in seconds that has been parsed from the response and can be manipulated as you want.
'access_token' => fn($response) => $response->access_token, // Should return the access token that has been parsed from the response.
'auth_type' => 'custom',
'apply_auth_token' => fn(PendingRequest $httpClient) => $request->withHeader('Authorization', 'Bearer ' . $token),
)->get(
'https://example.com/api',
);