使用适用于Google日历v3的Google API刷新令牌 [英] Refreshing Tokens with Google API for Google Calendar v3

查看:75
本文介绍了使用适用于Google日历v3的Google API刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google API(PHP)在系统日历和用户的Google日历之间执行每日同步.我希望系统用户能够从系统内部一次设置对日历的访问权限,然后系统可以通过cron作业每天同步日历.

I'm trying to use Google API (PHP) to perform a daily sync between a system calendar and the user's Google Calendars. I want a system user to be able to set up access to their calendar once from within the system, then the system can synchronise the calendars each day via a cron job.

我已经成功完成了从授权访问到同步日历的整个过程,但是我一直被令牌的到期时间所困扰.

I have managed to get the entire process working, from authorising the access to syncing the calendars, but I am getting stuck with the token expiry times.

我认为它在v3中的工作方式是将旧令牌用作刷新令牌.因此,一旦用户提供了授权,我只需要将旧令牌传递给Google,就可以运行同步了.

The way I thought it worked in v3 was the old token would act as the refresh token. So, once a user had provided authorisation, I just need to pass the old token to Google and it would allow me to run the sync.

这似乎可行,只要我在初始令牌的1小时到期时间内即可.一小时过后,它将停止工作,我需要让用户再次授权访问权限.

This seems to work, as long as I am within the 1 hour expiry time of the initial token. Once an hour passes, it stops working and I need to get the user to authorise access again.

是否可以让用户授权一次访问权限,然后每天同步一次日历,而无需用户再次授权访问?

Is there a way to get the user to authorise access once, then sync the calendars once per day without the user having to authorise access again?

谢谢!

推荐答案

好,在您获得Google授权期间,您会收到一个令牌,该令牌将在3600秒或一小时内过期,并且该令牌通常会过期.因此,您需要使用刷新令牌来获取新的工作令牌.

Well, during your authorization with Google, you will receive a token that will expire in 3600 seconds or one hour and it is normal to be expired. So you need to use refresh token to get a new working token.

这是您需要执行的步骤:

Here are the steps that you need:

$token = $client->getAccessToken();
$authObj = json_decode($token);
if(isset($authObj->refresh_token)) {
save_refresh_token($authObj->refresh_token);
}

保存此refresh_token很重要,然后您可以使用

It is important to save this refresh_token, then you can update it with

$client->refreshToken($your_saved_refresh_token);

然后将新的访问令牌设置为会话:

And then set your new access token to the session:

$_SESSION['access_token'] = $client->getAccessToken();

有关更多信息,请检查此 SO问题.

For more information, check this SO question.

这篇关于使用适用于Google日历v3的Google API刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆