Can't refresh access token for Google Calendar API on server side
Asked Answered
B

1

0

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

}
Bookstall answered 22/10, 2018 at 11:46 Comment(0)
N
0

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.

Newsdealer answered 23/10, 2018 at 9:33 Comment(1)
The solution was to use the same client when authorizing in the application and when updating the token on the server.Bookstall

© 2022 - 2024 — McMap. All rights reserved.