Hi
I recently worked through the - Google analytics API tutorial "Hello Analytics API" https://developers.google.com/analytics/resources/tutorials/hello-analytics-api

All went well to a point , I created the PHP in a file called googlestats.php and used require_once command to call it from my index.php page.

The Index page loads fine gives the button "Connect Me!" , which when clicked takes be to the 0auth2 require permission page on accounts.google.com.

When i click this i get "This web page is not available screen - The connection to www.mywebsite.com was interrupted."

cant work out where the code is wrong ?? the redirect URL is that of the PHP script, ie www.mywebsite.com/googlestats.php as it is set up on the API console.

I know this doesnt give a lot to work on - but any thought much appreciate!

<html>
<body>

<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiAnalyticsService.php';

$client = new apiClient();
$client->setApplicationName('Hello Analytics API Sample');

$client->setClientId('myclid.apps.googleusercontent.com');
$client->setClientSecret('mysecret');
$client->setRedirectUri('https://www.mywebsite.com/googlestats.php');
$client->setDeveloperKey('my dev key');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

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

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'>Connect Me!</a>";

} else {
  $analytics = new apiAnalyticsService($client);
  runMainDemo($analytics);
}

function runMainDemo(&$analytics) {
  try {

// Step 2. Get the user's first profile ID.
$profileId = getFirstProfileId($analytics);

if (isset($profileId)) {

  // Step 3. Query the Core Reporting API.
  $results = getResults($analytics, $profileId);

  // Step 4. Output the results.
  printResults($results);
}

  } catch (apiServiceException $e) {
    // Error from the API.
    print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();

  } catch (Exception $e) {
    print 'There was a general error : ' . $e->getMessage();
  }
}

function getFirstprofileId(&$analytics) {
  $accounts = $analytics->management_accounts->listManagementAccounts();

  if (count($accounts->getItems()) > 0) {
    $items = $accounts->getItems();
    $firstAccountId = $items[0]->getId();

$webproperties = $analytics->management_webproperties
    ->listManagementWebproperties($firstAccountId);

if (count($webproperties->getItems()) > 0) {
  $items = $webproperties->getItems();
  $firstWebpropertyId = $items[0]->getId();

  $profiles = $analytics->management_profiles
      ->listManagementProfiles($firstAccountId, $firstWebpropertyId);

  if (count($profiles->getItems()) > 0) {
    $items = $profiles->getItems();
    return $items[0]->getId();

  } else {
    throw new Exception('No profiles found for this user.');
  }
} else {
  throw new Exception('No webproperties found for this user.');
}
  } else {
    throw new Exception('No accounts found for this user.');
  }
}

function getResults(&$analytics, $profileId) {
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '2012-03-03',
       '2012-03-03',
       'ga:visits');
}

function printResults(&$results) {
  if (count($results->getRows()) > 0) {
    $profileName = $results->getProfileInfo()->getProfileName();
    $rows = $results->getRows();
    $visits = $rows[0][0];

print "<p>First profile found: $profileName</p>";
print "<p>Total visits: $visits</p>";

  } else {
    print '<p>No results found.</p>';
  }
}
?>

</body>
</html>

    How are you accessing this page in the first place? Is it via HTTPS or just HTTP? From the sounds of it I would guess that you are hitting your initial page under HTTP, then the redirect is specified as HTTPS but your server isn't set up to serve that page via HTTPS.

      I didn't notice that , if i change to HTTP , do i also change on the google api console to HTTP (i guess so ) and if so will this cause issues ??

      The google api console generated it i wth HTTPS.

      thanks

        so i changed the https to http on the website and on the google api portal...

        now i get this ??? xxmysite=, my web address.

        Fatal error: Uncaught exception 'apiAuthException' with message 'Error fetching OAuth2 access token, message: '<HTML> <HEAD> <TITLE>Error processing OAuth 2 request</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Error processing OAuth 2 request</H1> <H2>Error 500</H2> </BODY> </HTML> '' in /users/read/www.xxmysite.com/google-api-php-client/src/auth/apiOAuth2.php:105 Stack trace: #0 /users/read/www.xxmysite.com/google-api-php-client/src/apiClient.php(138): apiOAuth2->authenticate(Array) #1 /users/read/www.xxmysite.com/googlestats.php(18): apiClient->authenticate() #2 {main} thrown in /users/read/www.xxmysite.com/google-api-php-client/src/auth/apiOAuth2.php on line 105

          7 days later
          7 days later
          Write a Reply...