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;
}
?>
######################################################################