Google Analytics API permanent token

218 views
Skip to first unread message

Cristian Sannino

unread,
Mar 11, 2013, 7:26:38 AM3/11/13
to google-analytics...@googlegroups.com
Hi,
i have the entire example for google analytics api and work right, the only one big problem, is that i must connect every time, is possible help me to edit this script to work right for insert in db?

<?php
require_once 'Google_Client.php';
require_once 'Google_AnalyticsService.php';
require_once 'storage.php';
require_once 'authHelper.php';

$REDIRECT_URL = 'xxxxxxx';
$CLIENT_ID = 'xxxxxxxx';
$CLIENT_SECRET = 'xxxxxxxxxx';
$THIS_PAGE = 'index.php';
$APP_NAME = 'example';
$ANALYTICS_SCOPE = 'https://www.googleapis.com/auth/analytics.readonly';


$demoErrors = null;

$authUrl = $THIS_PAGE . '?action=auth';
$revokeUrl = $THIS_PAGE . '?action=revoke';

// Build a new client object to work with authorization.
$client = new Google_Client();
$client->setClientId($CLIENT_ID);
$client->setClientSecret($CLIENT_SECRET);
$client->setRedirectUri($REDIRECT_URL);
$client->setApplicationName($APP_NAME);
$client->setScopes(array($ANALYTICS_SCOPE));


// Magic. Returns objects from the Analytics Service
$client->setUseObjects(true);

// Build a new storage object to handle and store tokens in sessions.
// Create a new storage object to persist the tokens across sessions.
$storage = new apiSessionStorage();

$authHelper = new AuthHelper($client, $storage, $THIS_PAGE);


if ($_GET['action'] == 'revoke') { $authHelper->revokeToken();}
else if ($_GET['action'] == 'auth' || $_GET['code']) {$authHelper->authenticate();}
else{
  $authHelper->setTokenFromStorage();
 
  if ($authHelper->isAuthorized()) {
    $analytics = new Google_AnalyticsService($client);
   
      require_once 'CoreReportingApiReference.php';

      $demo = new coreReportingApiReference($analytics, $THIS_PAGE);
      $data_inizio='2013-02-01';
      $data_fine='2013-02-28';
      $htmlOutput = $demo->getHtmlOutput($_GET['tableId'],$data_inizio,$data_fine);
      $demoError = $demo->getError();
  }

  $storage->set($client->getAccessToken());
}

// Consolidate errors and make sure they are safe to write.
$errors = $demoError ? $demoError : $authHelper->getError();
$errors = htmlspecialchars($errors, ENT_NOQUOTES);
?>


<!DOCTYPE>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <?php
      // Print out authorization URL.
      if(!$authHelper->isAuthorized()) {
        print "<p><a href='$authUrl'>Connetti</a></p>";
      }


      if ($errors) {
        print "<div>There was an error: <br> $errors</div>";
      } else if ($authHelper->isAuthorized()) {
        print "<div>$htmlOutput</div>";
      }
    ?>
  </body>
</html>

Remco Borst

unread,
Mar 28, 2013, 7:15:55 AM3/28/13
to google-analytics...@googlegroups.com
I have the same problem.. I could not find that this is possible anywhere. Hopefully someone could come up with an answer.
Reply all
Reply to author
Forward
0 new messages