$client = new \Google_Client();
$client->setApplicationName('AnalyticsAPI');
$cred = new \Google_Auth_AssertionCredentials(
'1010008554258-XXXX@developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/analytics.readonly'),
\file_get_contents( __DIR__ . '/../google-keys/AnalyticsAPI-XXX.p12' )
);
echo 'test1/';
$client->setAssertionCredentials($cred);
echo 'test2';
if($client->getAuth()->isAccessTokenExpired()) {
echo 'test3/';
$client->getAuth()->refreshTokenWithAssertion($cred);
echo 'test4/';
}
The above php code spits out the following:
test1/test2/test3/An unexpected error has occurred. Please contact the webmaster.
This means that the error is happening when the code runs:
$client->getAuth()->refreshTokenWithAssertion($cred);
I have checked and triple checked the serviceaccount.com email as well as the p12 key.. but i get nothing other than, unexpected error.
Has anyone else hit this wall before? I cannot see what is wrong with the code.
Following the post: refreshTokenWithAssertion Permission Denied
I altered the above code where it was failing to run a try catch, but the try does not fail!:
if($client->getAuth()->isAccessTokenExpired()) {
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
echo 'Failed:';
print_r($e->getMessage());
}
}