Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>UPDATE July 21st, 2012</h2> <p>Google Analytics API V3 now supports OAuth2 tokens returned by a .p12-signed JWT request. That is, <strong>we can now use the Analytics API w/ service accounts</strong>.</p> <p>Currently pulling 4 years of day-by-day metrics, <em>just for the hell of it.</em></p> <p>Here's a quick 'n' dirty step-by-step:</p> <ol> <li><p>Go to the <a href="https://code.google.com/apis/console/">Google API Console</a> and create a new app</p></li> <li><p>In the <em>Services</em> tab, flip the <em>Google Analytics</em> switch</p></li> <li><p>In the <em>API Access</em> tab, click <em>Create an OAuth2.0 Client ID</em></p> <ul> <li><p>enter your name, upload a logo, and click <em>Next</em></p></li> <li><p>select the <em>Service account</em> option and press <em>Create client ID</em></p></li> <li><p>download your private key</p></li> </ul></li> <li><p>Now you're back on the <em>API Access</em> page. You'll see a section called <em>Service account</em> with a <em>Client ID</em> and <em>Email address</em></p> <ul> <li><p>Copy the email address (something like <em>####@developer.gserviceaccount.com</em>)</p></li> <li><p>Visit your <a href="https://www.google.com/analytics/web/#management/Accounts/">GA Admin</a> and <strong>add this email as a user to your properties</strong></p></li> <li><p>This is a must; you'll get cryptic errors otherwise.</p></li> </ul></li> <li><p>Get the latest <a href="https://github.com/google/google-api-php-client">Google PHP Client API</a> via Github</p> <pre><code>git submodule add https://github.com/google/google-api-php-client.git google-api-php-client-read-only </code></pre></li> <li><p>Rock 'n' roll (thanks all for tips on updated class names):</p> <pre><code>// api dependencies require_once(PATH_TO_API . 'Google/Client.php'); require_once(PATH_TO_API . 'Google/Service/Analytics.php'); // create client object and set app name $client = new Google_Client(); $client-&gt;setApplicationName(APP_NAME); // name of your app // set assertion credentials $client-&gt;setAssertionCredentials( new Google_Auth_AssertionCredentials( APP_EMAIL, // email you added to GA array('https://www.googleapis.com/auth/analytics.readonly'), file_get_contents(PATH_TO_PRIVATE_KEY_FILE) // keyfile you downloaded )); // other settings $client-&gt;setClientId(CLIENT_ID); // from API console $client-&gt;setAccessType('offline_access'); // this may be unnecessary? // create service and get data $service = new Google_Service_Analytics($client); $service-&gt;data_ga-&gt;get($ids, $startDate, $endDate, $metrics, $optParams); </code></pre></li> </ol> <p>&nbsp;</p> <p><sub><em>original workaround below</em></sub></p> <hr> <blockquote> <p>It seems that, despite ambiguous documentation, <a href="https://groups.google.com/forum/#!topic/oauth2-dev/NBQoOrtcC6A">most Google APIs do not support service accounts yet</a>, including Google Analytics. They cannot digest OAuth2 tokens returned by a .p12 signed JWT request. So, as of right now, you <em>cannot use Google Analytics API V3 with a service account</em>.</p> <p><strong>Workaround:</strong></p> <ol> <li><p>In the <a href="https://code.google.com/apis/console/b/0/?pli=1">Google API console</a>, create a <strong>client</strong> application.</p></li> <li><p>Follow the steps in the <a href="https://github.com/google/google-api-php-client">Google PHP Client API</a> examples to generate a <code>client_auth_url</code> using your <code>client_id</code>, <code>client_secret</code>, and <code>redirect_uri</code></p></li> <li><p>Login to <a href="https://accounts.google.com/ServiceLoginAuth">Google</a> using cURL. (Be sure to use a cookie file!)</p></li> <li><p>Open the <code>client_auth_url</code> in cURL and complete the form. Make sure you set <code>curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);</code> and <code>curl_setopt($ch, CURLOPT_HEADER, 1);</code> as the <code>authorization_code</code> will be in the <code>Location:</code> header of the response.</p></li> <li><p>Using your <code>client_id</code>, <code>client_secret</code>, <code>redirect_uri</code>, and the activation code from Step 4, post a request to the <a href="https://accounts.google.com/o/oauth2/token">Google's OAuth2 Token machine</a>. Make sure you include <code>grant_type = "authorization_code"</code> in your post fields.</p></li> <li><p>Hurray, you now have a <code>refresh_token</code> that never expires, and a working <code>access_token</code>! Post a request to the <a href="https://accounts.google.com/o/oauth2/token">Google's OAuth2 Token machine</a> with your <code>client_id</code>, <code>client_secret</code>, <code>redirect_uri</code>, and <code>refresh_token</code> when your <code>access_token</code> expires and you'll get a new one.</p></li> </ol> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload