无法在服务器端刷新Google Calendar API的访问令牌

时间:2018-10-22 11:46:39

标签: php google-calendar-api google-authentication refresh-token google-client

我在iOS上的客户端应用程序授权用户并接收访问令牌和刷新令牌,并将其发送到我的服务器,该服务器将其存储在数据库中。 服务器连接到日历并获取事件。 问题是使用刷新令牌不会以任何方式刷新访问令牌,并且会返回此错误:

{   “错误”:“ unauthorized_client”,   “ error_description”:“未经授权” }

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 个答案:

答案 0 :(得分:0)

您可以在响应unauthorized_client上检查related SO post。 请记住,必须通过authorizing/delegating your application在域的控制面板中授予对应用程序的访问权限,以免发生此错误。