Unlocking the Power of OAuth2 Authentication: A Step-by-Step Guide to Seamless Integration and Data Retrieval with the Laravel Saloon Package

Md Mazaharul Huq
2 min readOct 14, 2023

Step 1:

Run the this following function

composer require saloonphp/saloon “3.0”

composer require saloonphp/laravel-plugin “³.0”

php artisan vendor:publish — tag=saloon-config

Reference:

Now type the following command

php artisan saloon:connector Apps Quickbook — oauth

Now Change your Quickbook.php file as follow

namespace App\Http\Integrations\Apps;

use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\Connector;
use Saloon\Traits\OAuth2\AuthorizationCodeGrant;
use Saloon\Traits\Plugins\AcceptsJson;

class Gusto extends Connector
{
use AuthorizationCodeGrant;
use AcceptsJson;

/**
* The Base URL of the API.
*/
public function resolveBaseUrl(): string
{
return 'https://api.gusto-demo.com/';
}

/**
* The OAuth2 configuration
*/
protected function defaultOauthConfig(): OAuthConfig
{
return OAuthConfig::make()
->setClientId('')
->setClientSecret('')
->setRedirectUri('')
->setDefaultScopes([])
->setAuthorizeEndpoint('oauth/authorize')
->setTokenEndpoint('oauth/token')
->setUserEndpoint('user');
}
protected function defaultQuery(): array
{
return [
'per_page' => 100, // ?per_page=500
];
}

Now let’s build the controller

public function handleAuthorization()
{
$connector = new QuickBook();
$authorizationUrl = $connector->getAuthorizationUrl();
\Session::put('qb', $connector->getState());
return redirect()->to($authorizationUrl);
}

public function handleCallback(Request $request)
{
$code = $request->input('code');
$state = $request->input('state');
$expectedState = Session::pull('qb');
$connector = new Gusto();
$authorization = $connector->getAccessToken($code, $state, $expectedState);
$user = User::find(1);
$user->qb_auth= ($authorization);
$user->save();
return redirect()->route('home');
}

public function getPayroll()
{
$user = User::find(1);
$response = $user->authenticateQuickBooks()->send(new getDataRequest());
$tracks = $response->ok() ? $response->json() : null;
print_r($tracks);

//getDataRequest should be at the bottom

}

Let’s see the route file

Route::controller(QuickBookController::class)->prefix('apps/quickbooks')->name('spotify.')->group(function () {
Route::get('authorize', 'handleAuthorization')->name('authorize');
Route::get('callback', 'handleCallback')->name('callback');
Route::get('get_data', 'get_data')->name('get_data');
});

php artisan saloon:request QuickBooks getDataRequest

class PayrollRequest extends Request
{
/**
* The HTTP method of the request
*/
protected Method $method = Method::GET;

/**
* The endpoint for the request
*/
public function resolveEndpoint(): string
{
return '/v1/companies/00/payrolls';
}

Let’s Change User Model :

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'spotify_auth'
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'spotify_auth' => Serialized::class
];

public function authenticateQuickBooks(): QuickBook
{
$auth = $this->spotify_auth;

if ($auth->hasExpired()) {
$auth = QuickBook::make()->refreshAccessToken($auth);

$this->spotify_auth = $auth;
$this->save();
}

return QuickBook::make()->authenticate($auth);
}

Full example:

https://github.com/Sammyjo20/saloon-spotify-example

--

--