Skip to content

Instantly share code, notes, and snippets.

@fleeting
Created September 16, 2016 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleeting/77d42f954f9848366c2691df256ef931 to your computer and use it in GitHub Desktop.
Save fleeting/77d42f954f9848366c2691df256ef931 to your computer and use it in GitHub Desktop.
<?php
public function get_tokens() {
$google_client = new Google_Client();
$google_client->setAuthConfig(ROOT . '/mbcms_config/client_secrets.json');
$google_client->setAccessType("offline");
$google_client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$tokens = $this->GoogleAnalytic->find(
'list',
array(
'fields' => array(
'name', 'value'
)
)
);
$tokens['tokens'] = json_decode($tokens['tokens'], true);
$google_client->setAccessToken($tokens['tokens']);
if($google_client->isAccessTokenExpired()) {
$tokens['tokens']['refresh_token'] = $tokens['refresh_token'];
$google_client->setAccessToken($tokens['tokens']);
$google_client->fetchAccessTokenWithRefreshToken($google_client->getRefreshToken());
$new_tokens = json_encode($google_client->getAccessToken(), true);
$db = $this->GoogleAnalytic->getDataSource();
$this->GoogleAnalytic->updateAll(
array('value' => $db->value($new_tokens, 'string')),
array('name' => 'tokens')
);
$tokens['tokens'] = json_decode($new_tokens, true);
}
return $tokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment