how and when to use the refresh token with php google calender api

我与影子孤独终老i 提交于 2019-12-14 03:18:43

问题


I am using v3 of the php google calendar api.

After the calendar owner authorizes i save the access_token with the refresh token in the database.

When the calendar page is displayed by anyone i have this little bit of code that works

$client = new apiClient();
$client->setApplicationName("My Calendar");
$client->setAccessType('offline');
$client->setAccessToken( $_SESSION['google']['access_token'] );
$calService = new apiCalendarService($client);
$optParams = array('timeMin' => $gstart, 'timeMax'=> $gend);
$events = $calService->events->listEvents($_SESSION['google']['google_cal_id'], $optParams);

I have seen in the doc that at some point i may have to use the refresh token(which i have in the database.) What i do not know is how to use this token and when. Will setAccessToken thrown an exception at some point? What is the best way to test this as well Thanks


回答1:


The refresh token is used if you want to keep the authentication active without the user reauthenticating. For example, we have a reporting tool using google analytics api. I need to run queries on it every 10 minutes, so once the user authenticates our app I use the refresh token so that my automated queries can run even when the user is not logged in. Basically the refresh token is used to keep the session alive without user interaction. I hope that makes sense.




回答2:


The access token expires in 3600s or one hour. But you can use refresh token to get a new access token without user re-authenticate.

For details on how to get refresh token using php, check out the accepted answer in this post: Automatically refresh token using google drive api with php script

If you use the REST API instead of the PHP SDK, it's basically sending your refresh token, client_id, secret to https://accounts.google.com/o/oauth2/token as described at the bottom of the page here

Refresh token does NOT expire unless user revoke it or you programmatically revoke it.



来源:https://stackoverflow.com/questions/12011097/how-and-when-to-use-the-refresh-token-with-php-google-calender-api

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