Google Cal API - Script to Check Events

2019-02-26 06:58发布

问题:

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.

Thanks!

--

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?

回答1:

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