packagist saloonphp/saloon v1.3.0
Version v1.3 - OAuth2 Support 🔥

latest releases: dev-dependabot/composer/phpstan/phpstan-tw-1.11.4or-tw-2.0.0, v3.x-dev, v3.10.1...
2 years ago

Read the documentation here: https://docs.saloon.dev/advanced/oauth2-authentication

Version 1.3 introduces OAuth2 helpers/boilerplate code for the Authorization Code Flow! 🥳 It will allow you to write all the OAuth2 client configuration in a connector that you can use to:

  • Generate an authorization URL
  • Create access tokens
  • Create a refresh token

Connector

<?php

use Sammyjo20\Saloon\Helpers\OAuth2\OAuthConfig;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Traits\OAuth2\AuthorizationCodeGrant;

class SpotifyAuthConnector extends SaloonConnector
{
    use AuthorizationCodeGrant;

    public function defineBaseUrl(): string
    {
        return 'https://accounts.spotify.com';
    }

    protected function defaultOauthConfig(): OAuthConfig
    {
        return OAuthConfig::make()
            ->setClientId('my-client-id')
            ->setClientSecret('my-client-secret')
	    ->setDefaultScopes(['user-read-currently-playing'])
            ->setRedirectUri('https://my-app.saloon.dev/auth/callback')
	    ->setAuthorizeEndpoint('authorize')
            ->setTokenEndpoint('token')
            ->setUserEndpoint('user')
    }
}

Your application:

$authConnector = new SpotifyAuthConnector;

// 1. Redirect the user to the authorization URL...

$authorizationUrl = $authConnector->getAuthorizationUrl($scopes, $state);

// 2. Handle the callback from the API provider and create an access token...

$authenticator = $authConnector->getAccessTokens($code, $state);

// 3. Authenticate your requests!

$request = new GetTracksRequest;
$request->withAuth($authenticator);
$request->send(); // 🚀

// 4. Refresh your access tokens...

$newAuthenticator = $authConnector->refreshAccessTokens($authenticator);

Don't miss a new saloon release

NewReleases is sending notifications on new releases.