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

走远了吗. 提交于 2019-12-11 15:49:11

问题


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

}

回答1:


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.



来源:https://stackoverflow.com/questions/52928598/cant-refresh-access-token-for-google-calendar-api-on-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!