Okay, that's awesome. I've PARTIALLY gotten this to work. I can get
it to work really consistently with the apiPlusService, but it always
returns a 401 with apiAnalyticsService. Any idea why?
(see code below)
Again, THANK YOU so much for your help with this!!
-S.
//require the code to help you authenticate and access the api
require_once 'google-api-php-client/src/apiClient.php';
//require the code specific to the API service (ie. adsense,
analytics, google+, etc)
require_once 'google-api-php-client/src/contrib/
apiAnalyticsService.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
// Visit
https://code.google.com/apis/console to generate your
oauth2_client_id,
//oauth2_client_secret, and to register your oauth2_redirect_uri.
//For these purposes, you should set it up as if it was an "installed
application"
$client = new apiClient();
$client->setClientId('[client id]');
$client->setClientSecret(''[client secret]');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$service = new apiAnalyticsService($client);
$plus = new apiPlusService($client);
//modify this based on which type of api you want to dig into.
//look here:
http://code.google.com/apis/gdata/faq.html#AuthScopes
for help
$client->setScopes(array(
'
https://www.googleapis.com/auth/plus.me',
'
https://www.googleapis.com/auth/analytics.readonly'
));
//check to see if there's a saved token. The first time you run this
script, there won't be one
$token = file_get_contents("token.txt");
//if there isn't a token (as will happen the first time you run the
script
if (!($token)) {
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$authUrl = $client->createAuthUrl();
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
$token = $client->authenticate();
$write_token = file_put_contents("token.txt",$token);
//if there is a token
} else {
//test to see if the token has expired
$decoded_token = json_decode($token);
if (time() > ( $decoded_token->{'created'} + $decoded_token-
>{'expires_in'})) {
$client->refreshToken($decoded_token->{'refresh_token'});
$token = $client->getAccessToken();
}
$decoded_token = json_decode($token);
//now that we have a good token happening...
$client->setAccessToken($token);
//so this is working just beautifully
$activities = $plus->activities->listActivities('me', 'public');
print_r($activities);
//this is returning error message: "Error calling GET
https://www.googleapis.com/analytics/v3/management/accounts: (401)
Invalid Credentials"
$accounts = $service->management_accounts->listManagementAccounts();
print_r($accounts);
}