Google 日历 API 存储代码或访问令牌

Google calendar API storing code or access token

我试图弄清楚 Google 日历,但我无法弄清楚我需要在我的数据库中存储哪个字符串以在我需要推送或获取数据时获取访问令牌。

我向 Google 提出请求,他们 return:

?code=4/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#

然后我验证:

$client->authenticate($_GET['code']);

然后我得到访问令牌:

$client->getAccessToken()

对于这个 session 一切都很好。但是,我不想每次都要求用户进行身份验证。我可以存储 $_GET['code'] 字符串然后请求访问令牌而无需用户每次都进行身份验证吗?

谢谢。

是的,这绝对有可能。

您将希望从 OAuth 2.0 for Web Server Applicaitons 中了解 offline access。这将防止您的应用程序的用户每次都收到授权同意屏幕提示。

要使用 'offline access',您需要将以下代码片段应用到您的应用:

$client->setAccessType("offline");

您可能还想在此处阅读有关 OAuth 2.0 的更多信息 link:https://developers.google.com/identity/protocols/OAuth2

希望对您有所帮助,祝您好运!