Last active
February 5, 2026 09:01
-
-
Save chrispage1/a43069ed7df2b39128a530aa16076e60 to your computer and use it in GitHub Desktop.
Example implementation of motomedialab/connector with oAuth Client Credentials grant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Integrations; | |
| use Illuminate\Http\Client\Response; | |
| use Motomedialab\Connector\BaseRequest; | |
| use Motomedialab\Connector\Enums\RequestMethod; | |
| use Motomedialab\Connector\Contracts\RequestInterface; | |
| /** | |
| * @implements RequestInterface<array{token_type: string, access_token: string, expires_in: int}> | |
| */ | |
| readonly class ClientCredentialsRequest extends BaseRequest implements RequestInterface | |
| { | |
| public function method(): RequestMethod | |
| { | |
| return RequestMethod::POST; | |
| } | |
| public function endpoint(): string | |
| { | |
| return 'oauth/token'; | |
| } | |
| public function authenticated(): bool | |
| { | |
| return false; | |
| } | |
| public function body(): array | |
| { | |
| return [ | |
| 'grant_type' => 'client_credentials', | |
| 'client_id' => config('services.api.client_id'), | |
| 'client_secret' => config('services.api.client_secret'), | |
| 'scope' => config('services.api.scopes'), | |
| ]; | |
| } | |
| public function toResponse(Response $response): mixed | |
| { | |
| if ($response->ok()) { | |
| return $response->json(); | |
| } | |
| throw new \Exception('Failed to fetch client_credentials token. Status code ' . $response->getStatusCode()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Integrations; | |
| use Motomedialab\Connector\BaseConnector; | |
| use Illuminate\Http\Client\PendingRequest; | |
| /** | |
| * @see https://packagist.org/packages/motomedialab/connector | |
| */ | |
| class TestConnector extends BaseConnector | |
| { | |
| public function apiUrl(): string | |
| { | |
| return 'https://api.example.com/'; | |
| } | |
| public function authenticateRequest(PendingRequest $request): PendingRequest | |
| { | |
| $cacheKey = 'clientCredentialsToken'; | |
| // check if we already have our access token | |
| if ($cachedToken = cache()->memo()->get($cacheKey)) { | |
| return $cachedToken; | |
| } | |
| // fetch our client_credentials token | |
| $token = $request->withToken($this->send(new ClientCredentialsRequest())); | |
| // persist it in our cache | |
| cache()->put($cacheKey, $token['access_token'], $token['expires_in'] - 60); | |
| // authenticate our request with our new token | |
| return $request->withToken($token['access_token']); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment