How can I fetch an access token only when I get a 401 response? #77
-
I have two connectors, the first one is to fetch an access token and the second one is to interact with the resources.app\Http\Integrations\AutoConnect\AutoConnectAccessTokenConnector.php<?php
namespace App\Http\Integrations\AutoConnect;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
class AutoConnectAccessTokenConnector extends SaloonConnector
{
use AcceptsJson;
public function defineBaseUrl(): string
{
return config('services.autoconnect.oauthUrl');
}
} app\Http\Integrations\AutoConnect\AutoConnectConnector.php<?php
namespace App\Http\Integrations\AutoConnect;
use App\Http\Integrations\AutoConnect\Requests\GetAccessTokenRequest;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
class AutoConnectConnector extends SaloonConnector
{
use AcceptsJson;
// public function boot(SaloonRequest $request): void
// {
// $this->addResponseInterceptor(function (SaloonRequest $request, SaloonResponse $response) {
// if ($response->failed()) {
// // $authToken = GetAccessTokenRequest::make()->send()->body();
// $request->withTokenAuth(
// GetAccessTokenRequest::make()->send()->body()
// )->send();
// }
// });
// }
public function defineBaseUrl(): string
{
return config('services.autoconnect.baseUrl');
}
} Then I have my requests, the first one fetches the access token and the second one a list of states from the api.app\Http\Integrations\AutoConnect\Requests\GetAccessTokenRequest.php<?php
namespace App\Http\Integrations\AutoConnect\Requests;
use App\Http\Integrations\AutoConnect\AutoConnectAccessTokenConnector;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Traits\Plugins\HasJsonBody;
class GetAccessTokenRequest extends SaloonRequest
{
use HasJsonBody;
protected ?string $connector = AutoConnectAccessTokenConnector::class;
protected ?string $method = Saloon::POST;
public function defineEndpoint(): string
{
return '/oauth2/token';
}
public function defaultData(): array
{
return [
'client_id' => config('services.autoconnect.clientId'),
'client_secret' => config('services.autoconnect.clientSecret'),
'grant_type' => 'client_credential',
'scope' => 'SEMI',
];
}
} app\Http\Integrations\AutoConnect\Requests\ListStatesRequest.php<?php
namespace App\Http\Integrations\AutoConnect\Requests;
use App\Http\Integrations\AutoConnect\AutoConnectConnector;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
class ListStatesRequest extends SaloonRequest
{
protected ?string $connector = AutoConnectConnector::class;
protected ?string $method = Saloon::GET;
public function defineEndpoint(): string
{
return '/Catalogos/Ubicacion/Estados';
}
} From the docs I found that
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey! How does the token come back from the first request? Is the body JSON or is it just raw plain text that comes back from the API? |
Beta Was this translation helpful? Give feedback.
-
Figured it out! Instead of |
Beta Was this translation helpful? Give feedback.
Figured it out! Instead of
GetAccessTokenRequest::make()->send()->body()
, I had to callGetAccessTokenRequest::make()->send()->json()
.body()
was returning the token in wrapped in a excess pair of double quotes""super-secure-token-goes-here""
andjson()
just returns the plain string"super-secure-token-goes-here"
.