Created
March 3, 2015 16:24
-
-
Save ianbarber/d39c296f39a09f857db8 to your computer and use it in GitHub Desktop.
Query Analytics with Service Account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "../vendor/autoload.php"; | |
$client = new Google_Client(); | |
$client->setApplicationName("Service Acccount Test"); | |
$client_id = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c.apps.googleusercontent.com'; | |
$service_account_name = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c@developer.gserviceaccount.com'; | |
$key_file_location = 'foostarter-a27f84b2dec4.p12'; | |
$key = file_get_contents($key_file_location); | |
$cred = new Google_Auth_AssertionCredentials( | |
$service_account_name, | |
array(Google_Service_Analytics::ANALYTICS_READONLY), | |
$key | |
); | |
// Remember to cache the access token locally if making multiple calls | |
// and don't just use this function for each request! | |
$client->getAuth()->refreshTokenWithAssertion($cred); | |
$service = new Google_Service_Analytics($client); | |
$result = $service->data_ga->get("ga:62716028", "yesterday", "today", "ga:pageviews"); | |
var_dump($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment