Created
September 16, 2016 20:02
-
-
Save fleeting/77d42f954f9848366c2691df256ef931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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