My client application on iOS authorizes the user and receives the access token and the refresh token and sends it to my server, where it is stored in the database. The server connects to the calendar and get the events. The problem is that the access token is not refreshed in any way using the refresh token and this error is returned:
{ "error": "unauthorized_client", "error_description": "Unauthorized" }
public function sync($token, $expiresIn, $refreshToken, $created)
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar Synchroniser');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAuthConfig('../client_secret_***************.apps.googleusercontent.com');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setAccessToken(json_encode([
'access_token' => $token,
'created' => $created,
'expires_in' => $expiresIn,
'refresh_token' => $refreshToken
], true));
//create google calendar service
$service = new Google_Service_Calendar($client);
//filter for events searching
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'singleEvents' => true
);
//get events
$results = $service->events->listEvents($calendarId, $optParams);
}