对于发现此问题的任何人-此错误是由于在 win7 机器上下载后 .p12 已损坏。下载到unix机器后,证书工作了。
我遵循了大约一百万个不同的指南,试图让它发挥作用。
我正在尝试使用服务帐户连接到Google Analytics API。
我在控制台中创建了相关的“项目”,因为所述项目需要访问相关 API。
在凭据中,我在 OAuth 部分添加了服务帐户,下载了 p12 密钥并存储在服务器上。
当我运行代码时:
//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);
错误是从“第 52 行的 Google/Signer/P12.php”引发的
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
错误是从 ...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()
);
}
当我提取试图读取 .p12 文件并自行运行的相关代码时,我得到了同样的错误:
$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
我完全被难住了。阅读有关该问题的其他一些帖子,例如: Getting "Unable to parse the p12 file..." Error With google-api-php-client
我努力了
- 确保权限正确。
file_get_contents($key)
然后传递给openssl_pkcs12_read
which 会产生相同的结果!
有没有人有任何线索?