使用google-api-php-client获取"无法解析p12文件..."错误

the*_*wts 9 google-api-php-client

我一直在寻找几天,并尝试新的和旧版本的google-api-php-client以及各种其他示例,但我似乎无法绕过这个错误.下面的代码是从GitHub检索到的服务帐户示例,我从API控制台中删除了我的凭据和文件.我有一个我正在构建的不同文件,但是为了在这个问题中使用,我认为这个更简单的文件会更容易讨论.我对这两个文件都有同样的错误.

致命错误:未捕获的异常'Google_Auth_Exception',消息'无法解析p12文件.这是一个.p12文件吗?密码是否正确?OpenSSL错误:第52行的/google-api-php-client/src/Google/Signer/P12.php

我完全不知道为什么会抛出这个错误.

我已经验证"file_get_contents"实际上是获取文件的内容,我的"notasecret"密码正在被正确拉出.我希望这个最近的提交可能有所帮助,但不幸的是,它并没有为我解决这个错误.

知道这里出了什么问题吗?谢谢你的任何建议!

<?php

session_start();
include_once "templates/base.php";

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';


$client_id = 'xxxx.apps.googleusercontent.com';
$service_account_name = 'xxxx@developer.gserviceaccount.com';
$key_file_location = 'xxxx-privatekey.p12';

echo pageHeader("Service Account Access");
if ($client_id == 'xxxx.apps.googleusercontent.com'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Books($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/books'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();


$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";
}

echo pageFooter(__FILE__);
Run Code Online (Sandbox Code Playgroud)

the*_*wts 1

知道了!这次提交让我想到也许 openssl 也不喜欢我们的文件。我使用这些说明将文件从 p12 转换为 pem 。然后我编辑该文件以删除“-----BEGIN RSA PRIVATE KEY-----”之前的一些文本,以便新提交中的代码可以正确识别它。将该文件放入并获得神奇的结果。

就是这样!我仍然不确定什么会导致自动生成的 p12 文件不合作,所以如果有人有任何想法请告诉我。