Hello, I’m trying to refresh my API token using a PHP Laravel setup and am running into some problems. I’ve tried a lot of times, but I keep getting the invalid client (401) error. Now, I’ve made double-sure that the values stored in DB and env file are correct, but am likely not using something correctly.
How I began the process was by obtaining the access code through browser and then sending a CURL POST request from the registered domain’s command line. Once I have the access_token
and refresh_token
, I’m storing it in DB through seeder (this is a one time process), and then have a Cron job that goes:
$infs = InfsToken::find(1);
//if last updated + timeout > current time, update
$last_refresh_time = Carbon::parse($infs->updated_at)->addSeconds($infs->refresh_timeout);
if(Carbon::now() > $last_refresh_time) {
$url = env('INFS_API_BASE_URL') . '/token';
$auth_string = env('INFS_CLIENT_ID') . ':' . env('INFS_CLIENT_SECRET');
$auth_string = 'Basic' . base64_encode($auth_string);
$client = new \GuzzleHttp\Client();
$requestBody = [
'grant_type' => 'refresh_token',
'refresh_token' => $infs->refresh_token,
];
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => $auth_string
],
'form_params' => $requestBody
]);
$responseBody = $response->getBody();
$infs->access_token = $responseBody['access_token'];
$infs->refresh_token = $responseBody['refresh_token'];
$infs->save();
Log::info('Got new token:' . $infs->access_token);
}
Please do help me as I’m feeling lost. Feel free to ask any questions or request more info. Thanks in advance!