FOR ANYONE FINDING THIs - this error was due to the .p12 being corrupt after downloading on a win7 box. After downloading to a unix machine the cert worked.
I have followed about a million different guides trying to get this to work.
I am trying to connect to the Google Analytics API with a service account.
I have the relevant "project" created in the console, given said project access to the relavent API's required.
In the credentials I have added the service account in the OAuth section, downloaded the p12 key and stored on server.
When I run the code:
//start the google v3 api server authorization with the .p12 key
$client = new \Google_Client();
$client->setApplicationName("AnalyticsAPI");
$key = __DIR__ . '/google-keys/AnalyticsAPI-XXXXXX.p12';
$credentials = new \Google_Auth_AssertionCredentials(
'101XXXXXXXXXXXXXXXXXXXXXnq4omne@developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/analytics.readonly'),
$key
);
$client->setAssertionCredentials($credentials);
//auto refresh if old
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credentials);
}
//start the analytics shtuff
$service = new \Google_Service_Analytics($client);
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
//Adding Dimensions
$params = array('dimensions' => 'ga:pagePath');
// requesting the data
$data = $service->data_ga->get("ga:$profile_id", $start_date, $end_date, "ga:users,ga:sessions", $params );
print_r($data);
The error is thrown from the "Google/Signer/P12.php on line 52"
Unable to parse the p12 file. Is this a .p12 file? Is the password correct? OpenSSL error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag in /XXXXX/classes/google-api-php-client-master/src/Google/Signer/P12.php on line 52
The error is thrown from ...Signer/P12.php @ 49:
// This throws on error
$certs = array();
if (!openssl_pkcs12_read($p12, $certs, $password)) {
throw new Google_Auth_Exception(
"Unable to parse the p12 file. " .
"Is this a .p12 file? Is the password correct? OpenSSL error: " .
openssl_error_string()
);
}
When i extract the relavent code attempting to read the .p12 file and run on its own i get the same error:
$certs = array();
openssl_pkcs12_read($key, $certs, 'notasecret');
print_r($certs);
echo openssl_error_string();
die(x);
Array ( ) error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
I'm completely stumped. Reading some other posts on the issue like this one: Getting "Unable to parse the p12 file..." Error With google-api-php-client
I have tried
- Ensuring permissions are correct.
file_get_contents($key)
and then passing to theopenssl_pkcs12_read
which yields the same results!
Does anyone have any clues?