Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error refreshing the OAuth2 token { "error" : "invalid_grant" }

please help me to understand where mistake.

API

Google Api PHP v3, key.p12 is the file in the .zip collection API (or where i find it ?)

I masked my data with xxxxxxxxxx

Excuse me for my inglish

gaphp.php

<?php

      require_once 'Google/Client.php';
      require_once 'Google/Service/Analytics.php';

      $client_id = 'xxx-xxx.apps.googleusercontent.com';
      $service_account_name = '[email protected]';
      $keyfile = 'key.p12';
      $redirect_url = 'http://example.com/service/ga/gaphp.php';
      $client_secret = 'xxxxxxxxxxxxxxxxxxxxx';

      // Initialise the Google Client object
      $client = new Google_Client();
      $client->setApplicationName('My application');
      $client->setRedirectUri($redirect_url);
      $client->setClientSecret($client_secret);

      $client->setAssertionCredentials(
              new Google_Auth_AssertionCredentials(
                  $service_account_name,
                  array('https://www.googleapis.com/auth/analytics'),
                  file_get_contents($keyfile)
              )
      );

      // Get this from the Google Console, API Access page
      $client->setClientId($client_id);
      $client->setAccessType('offline_access');
      $analytics = new Google_Service_Analytics($client);

      // We have finished setting up the connection,
      // now get some data and output the number of visits this week.

      // Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
      $analytics_id   = 'ga:xxxxxx';
      $lastWeek       = date('Y-m-d', strtotime('-1 week'));
      $today          = date('Y-m-d');

      try {
          $results = $analytics->data_ga->get($analytics_id, $lastWeek, $today,'ga:visits');
          echo '<b>Number of visits this week:</b> ';
          echo $results['totalsForAllResults']['ga:visits'];
      } catch(Exception $e) {
          echo 'There was an error : - ' . $e->getMessage();
      }

?>

Error

There was an error : - Error refreshing the OAuth2 token, message: 'Protection: 1; mode=block Server: GSE Alternate-Protocol: 443:quic Transfer-Encoding: chunked { "error" : "invalid_grant" }'
like image 310
Leonardo Ciaccio Avatar asked Apr 02 '26 20:04

Leonardo Ciaccio


1 Answers

I just got my analytics working with a service account yesterday - so I might be able to help. From the console, https://console.developers.google.com/project, you can select your project and go to "APIS & AUTH". Make sure analytics API is turned on. And make sure you have service account "credentials". Make sure to grant the service account email address permissions for your google analytics property. You will also need to get the P12 key file for your service account and provide that key file on your server. I actually use the complete file path for my $key_file_location variable.

I used this code on GitHub: https://github.com/google/google-api-php-client/blob/master/examples/service-account.php

The example is for the Books API - so just replace "Books" with "Analytics" and "books" with "analytics". Fill in your service account credentials and remember to add your service account email to your google analytics property.

like image 198
Gavin Palmer Avatar answered Apr 18 '26 23:04

Gavin Palmer