Google Cal API - Script to Check Events

心已入冬 提交于 2019-12-02 02:35:46

To enable your script to be called beyond the lifetime of the original access token (which only last an hour), you will need to retrieve and store the refresh token during the initial authorisation, and then use this to generate a new access token each time your script runs.

Access tokens have limited lifetimes. If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

https://developers.google.com/identity/protocols/OAuth2#basicsteps (4. Refresh the access token, if necessary.)

After the user has authenticated your app, they are returned to your redirect URI with the code query-string.

The following is an example of authenticating and getting the refresh token (this uses Analytics, but should be the same for other services):

$client = new Google_Client();
$client->setClientId('xxx');
$client->setClientSecret('xxx');
$client->setRedirectUri('xxx');

//authenticate with the code returned from google
$authenticate = $client->authenticate($_GET['code']);

//get the refresh token
$refreshToken = $client->getRefreshToken();

//store refresh token in database...

Then, when you run your daily script, use the refresh token (retrieved from your database) to generate a new access token:

$client = new Google_Client();
$client->setClientId('xxx');
$client->setClientSecret('yyy');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

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