Php 使用Google_客户端的Google Analytics API中的OAUTH2签名错误

Php 使用Google_客户端的Google Analytics API中的OAUTH2签名错误,php,oauth-2.0,google-analytics-api,google-api-client,Php,Oauth 2.0,Google Analytics Api,Google Api Client,在尝试使用OATH2对Google Analytics API(v3)进行身份验证时,我遇到以下错误: 警告:openssl_sign()要求参数4为长字符串,在第60行的google api php client/src/auth/google_P12Signer.php中给出 Google_AuthException:无法在第61行的Google api php client/src/auth/Google_P12Signer.php中对数据进行签名 以下是我的PHP代码: // api d

在尝试使用OATH2对Google Analytics API(v3)进行身份验证时,我遇到以下错误:

警告:openssl_sign()要求参数4为长字符串,在第60行的google api php client/src/auth/google_P12Signer.php中给出

Google_AuthException:无法在第61行的Google api php client/src/auth/Google_P12Signer.php中对数据进行签名

以下是我的PHP代码:

// api dependencies
require_once(dirname(__FILE__) . '/../../vendor/google-api-php-client/src/Google_Client.php');
require_once(dirname(__FILE__) . '/../../vendor/google-api-php-client/src/contrib/Google_AnalyticsService.php');

session_start();

// create client object and set app name
$client = new Google_Client();
$client->setApplicationName(APP_NAME);

// set assertion credentials
$client->setAssertionCredentials(
  new Google_AssertionCredentials(
    'xxxxxxx@developer.gserviceaccount.com',
    array('https://www.googleapis.com/auth/analytics.readonly'),
          file_get_contents('xxxxxxxxxx-privatekey.p12')  // keyfile
));

// other settings
$client->setClientId('xxxxxxx.apps.googleusercontent.com');
$client->setAccessType('offline_access');

// create service
$service = new Google_AnalyticsService($client);

$properties = $service->management_webproperties->listManagementWebproperties("~all");
print_r($properties);
如果我打印($service),我会得到一个没有错误的有效对象。生成错误的是listManagementWebproperties()调用


有人有解决办法吗?看起来Google_客户端可能正在变化,因为它是在几天前编辑的。我通过SVN从trunk获得它,而不是通过下载页面,我相信下载页面有一个不支持服务帐户的旧版本。谢谢。

根据
openssl\u sign()
PHP文档,第四个参数应该是int

在代码中,他们是这样使用的:

if (!openssl_sign($data, $signature, $this->privateKey, "sha256")) {
  throw new Google_AuthException("Unable to sign data");
}
传递“sha256”,这显然不是一个INT。没有为sha256定义OPENSSL算法:这意味着它不起作用

您需要做的是定义自己的OPENSSL_ALGO_SHA256,就像您在下面的代码中看到的那样:


也有人写了一个补丁来帮助:

谢谢。我会按照你的建议尝试解决方法,但这是他们需要在Google_P12Signer中进行的修复。如果他们一直在测试,我怀疑他们已经知道这个bug:)我同意……不知道他们为什么这样做。更新:“sha256”问题在PHP5.3.x中消失了。openssl_符号已更改为允许在第4个参数中使用字符串。第4个参数可以是PHP5.3中的字符串。你会想,到现在为止,这将在文件中传达。
if (!openssl_sign($data, $signature, $this->privateKey, "sha256")) {
  throw new Google_AuthException("Unable to sign data");
}