php - Can't refresh access token for Google Calendar API on server side

674

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);

}
597

Answer

Solution:

You can check this related SO post about the response “unauthorized_client”. And remember that you must give access to your application in the control panel of your domain by authorizing/delegating your application to avoid this error.

People are also looking for solutions to the problem: PHP uploading files image only

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.