oauth2callback.php for google-api-php-client-0.5.0

1,656 views
Skip to first unread message

alessandro littera

unread,
Oct 9, 2012, 7:33:36 AM10/9/12
to google-analytics...@googlegroups.com
hi I'm a italian web developer,

I'm starting to use the google-api-php-client, I downloaded the libraries version 0.5.0

I have loaded all the site and wanted to start analyticts starting from Google.
My problem is that I do not know what code to use in the file redirection oauth2callback.php
 
I'll post my code to ask authorization to serve GA:

############################## CODE ##############################

<?php
//Includo la libreria delle API di Google
require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiAnalyticsService.php';
 
//Avvio della sessione
session_start();
 
$client = new apiClient();
$client->setApplicationName('Hello Analytics API Sample');
 
$tableId = 'ga:XXXXXX';
 
//Informazioni account
$client->setClientId('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('XXXXXXXXXXXXXXXXXXXXXXXX/oauth2callback.php');
$client->setDeveloperKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$client->setScopes(array('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'));
 
$client->setUseObjects(true);
 
//Recupero del token
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
 
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
 
if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Autenticati!</a>";
 
//esecuzione della funzione
} else {
  $analytics = new apiAnalyticsService($client);
  $results = queryCoreReportingApi ($analytics, $tableId);
  getFormattedResults($results);
}
 
//interrogazione del servizio
function queryCoreReportingApi(&$analytics, $tableId) {
 
    $optParams = array(
        'dimensions' => 'ga:source,ga:keyword',
        'sort' => '-ga:visits,ga:keyword',
        'filters' => 'ga:medium==organic',
        'max-results' => '25');
 
    return $analytics->data_ga->get(
        urldecode($tableId),
        '2012-09-15',
        '2012-09-30',
        'ga:visits',
        $optParams);
  }
 
 
 
 
//formattazione dei risultati
function getFormattedResults($results) {
    $profileName = $results->getProfileInfo()->getProfileName();
    $output = '<h3>Results for profile: '
        . htmlspecialchars($profileName, ENT_NOQUOTES)
        . '</h3>';
    if (count($results->getRows()) > 0) {
      $table = '<table>';
 
      // Header della tabella
      $table .= '<tr>';
      foreach ($results->getColumnHeaders() as $header) {
        $table .= '<th>' . $header->getName() . '</th>';
      }
      $table .= '</tr>';
 
      // Righe della tabella
      foreach ($results->getRows() as $row) {
        $table .= '<tr>';
          foreach ($row as $cell) {
            $table .= '<td>'
                   . htmlspecialchars($cell, ENT_NOQUOTES)
                   . '</td>';
          }
        $table .= '</tr>';
      }
      $table .= '</table>';
    } else {
      $table = '<p>No results found.</p>';
    }
    echo $output . $table;
  }
?>
######################################################################

Jeff571

unread,
Oct 15, 2012, 12:01:30 AM10/15/12
to google-analytics...@googlegroups.com
Hi Alessandro, this is Jeff from Oregon in the US.  Perhaps you've already found your answer, but just in case,  I am new to the google-api-php-client as well and have been struggling this past week with similar questions. I finally realized that the redirect URI needs to be, at least in my case, the path and php filename of the script making the call. I found a tutorial that walked through the steps and called the google analytics api from a script called 'apitest.php'.  Therefore, in that script, the value of the redirectURI parameter was set to'http://your directory/apitest.php'.  You can also put more  than one redirect URI (each one on a separate line).  I'm now on to my next issue which is when you are presented with the access screen, and hit the left button to grant access, my page times out.
Reply all
Reply to author
Forward
0 new messages