Google Cal API-检查事件的脚本 [英] Google Cal API - Script to Check Events

查看:129
本文介绍了Google Cal API-检查事件的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个脚本,该脚本将通过Google Calendar PHP Client检查经过身份验证的用户的Google Calendar.我能够构建一个简单的页面,允许用户进行身份验证并授予日历事件"权限.我收到令牌,然后通过以下方式抓取15个即将发生的事件:

I want to build a script that will check a Authenticated User's Google Calendar via the Google Calendar PHP Client. I was able to build a simple page that lets a user Auth and give permission to the Calendar Events. I receive a token and then grab a 15 upcoming events via:

$googleCal = new Google_Service_Calendar($googleClient);
$results = $googleCal->events->listEvents($calendarId, $optParams);

但是我正在苦苦挣扎的是如何保存它,因此我可以让脚本每天检查一次,以查看是否添加了新事件.我想我已经快要克服终点线了.

But what I'm struggling with is how to save this so I can have a script check this everyday to see if there were new events added. I think what I have is close just struggling to get over the finish line.

谢谢!

-

更新,我正在尝试使用刷新令牌,这是我的代码:

Update, I'm trying to use the refresh token, here is my code:

public function checkRedirectCode()
  {
        if(isset($_GET['code']))
        {
              $this->client->authenticate($_GET['code']);

        //    $this->setToken($this->client->getRefreshToken());
              $this->setToken($this->client->getAccessToken());

              $this->storeUser($this->getPayload());

              return true;
        }

        return false;
  }

  public function setToken($token)
  {
              $_SESSION['access_token'] = $token;
              $this->client->setAccessToken($token);

  }

我能够回显刷新令牌,所以我知道我得到了正确的刷新令牌,但是每当使用注释掉的字符串时,我都会出错.有什么想法吗?

I have been able to echo the refresh token so I know I'm getting a proper refresh token but I'm getting errors whenever I use the commented out string. Any ideas?

推荐答案

要在原始访问令牌的生存期(仅持续一个小时)之后调用脚本,您将需要检索并存储刷新令牌在初始授权期间,然后在每次脚本运行时使用它来生成新的访问令牌.

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.

访问令牌具有有限的生存期.如果您的应用程序需要访问 超出单个访问令牌的生存期的Google API,它可以 获取刷新令牌.刷新令牌使您的应用程序可以 获取新的访问令牌.

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.如有必要,刷新访问令牌.)

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

用户对您的应用进行身份验证后,它们将使用code查询字符串返回到您的重定向URI.

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

以下是验证和获取刷新令牌的示例(此示例使用Google Analytics(分析),但对于其他服务应相同):

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);

这篇关于Google Cal API-检查事件的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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