Google API oauth2 令牌过期?

Google API oauth2 token expires?

我对 google api 和 oauth2 令牌有疑问。

有一个应用程序允许通过 oauth2 令牌将联系人/日历与您的 google 帐户同步。

当用户第一次想连接他的 google 帐户时,他需要授予访问权限,然后应用程序正在接收 code/token 并保存并稍后用于离线同步。

function getClient($app) 
{       
    $client = new Google_Client();
    $client->setAuthConfig("path_to_secret.json");

    switch($app)
    {
        case 'contacts':
        $client->addScope(Google_Service_Script::WWW_GOOGLE_COM_M8_FEEDS);
        $client->addScope(Google_Service_People::USERINFO_EMAIL);
        break;

        case 'calendar':
        $client->addScope(Google_Service_Calendar::CALENDAR);
        break;

        default:
        throw new Exception('API Callback not defined in setup');
    }

    $client->setAccessType('offline'); // offline access
    $client->setIncludeGrantedScopes(true);   // incremental auth
    $client->setRedirectUri(GOOGLE_APP_URL . $app.'/callback.php');
    return $client;
}

(联系人和日历有不同的令牌)

同步脚本:

...
try
{
    $client = getClient('calendar');
    $client->setAccessToken(unserialize($accessToken));
    $http = $client->authorize();

    $service = new Google_Service_Calendar($client);

    ...
}

$accessToken 是一个序列化字符串,如:

a:5:{s:12:"access_token";s:131:"******token_here********";s:10:"token_type";s:6:"Bearer";s:10:"expires_in";i:3598;s:8:"id_token";s:902:"***id_token****";s:7:"created";i:1505178047;}

这是第一次工作,后来又工作了几次,但过了一段时间(几小时)后出现错误:

Error: {"error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" }}

我做错了什么?

有趣的是,联系人同步一直运行良好(访问令牌具有与日历同步相同的属性)

好的,应该解决了 - refresh_token 只是第一次提供,所以当我多次测试它时,我没有得到刷新令牌。 当我在 https://myaccount.google.com/u/0/permissions 中撤销访问权限并再次连接时,我也收到了刷新令牌。我假设现在它会正常工作