EasyOauth is a good starter pack for implement API with Oauth2 in your project. EasyOauth manage token needed for your api.
Build on Symfony 5
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
$ composer require easyoauth
Here is an example for a famous French job api:
http_client:
...
scoped_clients:
oauth.client:
scope: 'https://\entreprise\.pole-emploi.fr'
headers:
Content-Type: application/x-www-form-urlencoded
Accept: application/json
###> EasyOauth/EasyOauth ###
CLIENT_ID=
CLIENT_SECRET=
CLIENT_URI= //Sample: https://entreprise.pole-emploi.fr/connexion/oauth2/access_token?realm=%2Fpartenaire
CLIENT_SCOPE=""
###< EasyOauth/EasyOauth ###
Symfony console d:s:u --force
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
EasyOauth\src\EasyOauthBundle::class => ['all' => true]
Retreive oauthConnect() service with dependency injection into your controller
use EasyOauth\src\Provider\OauthProvider;
//...
//Generate Token
$this->oauthProvider->oauthConnect(true); //If scope is defined
$token = $this->oauthProvider->getToken();
//Start your call
Enjoy :)