Been racking my brain with this all day. Not sure why I'm seeing this error in production but no error when testing locally.
{
"message": "refresh token must be passed in or set as part of setAccessToken",
"exception": "LogicException",
"file": "/var/www/laravel/vendor/google/apiclient/src/Google/Client.php",
"line": 266,
"trace": [
{
"file": "/var/www/laravel/vendor/google/apiclient/src/Google/Client.php",
"line": 254,
"function": "fetchAccessTokenWithRefreshToken",
"class": "Google_Client",
"type": "->"
},
{
"file": "/var/www/laravel/app/Services/GoogleService.php",
"line": 50,
"function": "refreshToken",
"class": "Google_Client",
"type": "->"
}
}
My setup is as follow:
class GoogleService {
protected $client;
protected $service;
function __construct() {
/* Get config variables */
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . resource_path(config('google.credentials')));
$scopes = array(
...
);
$this->client = new \Google_Client();
$this->client->useApplicationDefaultCredentials();
$this->client->setApplicationName(config('google.app_name'));
$service_account = config('google.service_account_name');
$this->client->setSubject($service_account);
$this->client->setScopes($scopes);
$this->client->setAccessType('offline');
$this->client->setApprovalPrompt('force');
$this->service = new \Google_Service_Directory($this->client);
/* If we have an access token */
if (Cache::has('service_token')) {
$this->client->setAccessToken(Cache::get('service_token'));
}
if ($this->client->isAccessTokenExpired()) {
$this->client->refreshToken(Cache::get('service_token'));
}
Cache::forever('service_token', $this->client->getAccessToken());
}
public function verify($user_id)
{
try {
$result = $this->service->users->get($user_id);
} catch (\Google_Service_Exception $e) {
Log::error($e->getMessage());
return response()->json(['error' => 'There was a general error : ' . $e->getMessage()], 404);
}
return $result;
}
public function get($user_id)
{
$optParams = array('projection' => 'full');
$user = $this->service->users->get($user_id, $optParams);
if ($user_id !== $user->primaryEmail) {
return response()->json(['error' => 'Unauthorized - Bad credentials.'], 401);
}
return $user;
}
}
I'm finally done working locally using vagrant/homestead on my mac and I try to load to production and am unable to run this. I've found some other posts about errors but it seems to only be for client to server and in this instance is server to server. I wonder if that makes a difference.
Any help would be appreciated.
Thanks all!