【问题标题】:OAUTH2 signing error in Google Analytics API using Google_Client使用 Google_Client 的 Google Analytics API 中的 OAUTH2 签名错误
【发布时间】:2012-08-04 18:48:39
【问题描述】:

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

警告:openssl_sign() 期望参数 4 很长,字符串在 google-api-php-client/src/auth/Google_P12Signer.php 第 60 行给出

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);

如果我 print_r($service) 我得到一个没有错误的有效对象。产生错误的是 listManagementWebproperties() 调用。

请问有大神解答吗?看起来 Google_Client 可能在不断变化,因为它是在几天前编辑的。我通过 SVN 从主干获得它,而不是通过下载页面获得它,我相信它有一个不支持服务帐户的旧版本。谢谢。

【问题讨论】:

    标签: php oauth-2.0 google-analytics-api google-api-client


    【解决方案1】:

    根据openssl_sign() PHP 文档,第四个参数应该是一个int

    http://php.net/manual/en/function.openssl-sign.php

    bool openssl_sign ( string $data , string &$signature , mixed $priv_key_id [, int $signature_alg = OPENSSL_ALGO_SHA1 ] )
    

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

    if (!openssl_sign($data, $signature, $this->privateKey, "sha256")) {
      throw new Google_AuthException("Unable to sign data");
    }
    

    传递显然不是 INT 的“sha256”。根据以下内容,没有为 sha256 定义 OPENSSL_ALGO_*:http://www.php.net/manual/en/openssl.signature-algos.php,这意味着它不起作用。

    您需要做的是定义自己的 OPENSSL_ALGO_SHA256,有点像您在以下代码中看到的:http://pastebin.com/qdCyC0Pe

    也有人写了一个补丁来帮忙:http://php.it2y.info/missing-sha256-sha512-families-of-signature-algorithms.html

    【讨论】:

    • 谢谢。我会按照您的建议尝试解决方法,但这是他们需要在 Google_P12Signer 中进行的修复。如果他们一直在测试它,我怀疑他们已经知道这个错误:)
    • 我同意...不知道他们为什么这样做。
    • 更新:“sha256”问题在 PHP 5.3.x 中消失了。 openssl_sign 已更改为允许在第 4 个参数中使用字符串。
    • 第 4 个参数在 PHP 5.3 中可以是字符串。 markmail.org/message/6dpa2dqtm6m2pspu
    • 您现在会认为这将在文档中进行沟通。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2014-07-09
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    相关资源
    最近更新 更多