使用PHP的OAuth Google Analytics分析身份验证

问题描述

|| 我正在尝试让OAuth与Google Analytics(分析)一起使用,但是我没有任何运气。我的代码如下所示:
require_once(\'common.inc.PHP\');

$consumerKey = \"my.url.net\";
$consumerSecret = \"nzZ....TX\";
$sig_method = \"HMAC-SHA1\";
$oauth_token = $token ? new OAuthToken($token,$token_secret) : NULL;
$token_endpoint = \'/accounts/OAuthGetRequestToken\';
$scope = \'https://www.google.com/analytics/Feeds/\';
$callback_url = \"http://my.url.net/pete/oauth/\";
$privKey = NULL;
$params = array(\'scope\' => $scope,\'oauth_callback\' => $callback_url);
$url = $token_endpoint . \'?scope=\' . urlencode($scope);

$consumer = new OAuthConsumer($consumerKey,$consumerSecret);

$req = OAuthRequest::from_consumer_and_token($consumer,$oauth_token,\'GET\',$url,$params);

//$req->sign_request($sig_method,$consumer,NULL);

echo \"<PRE>\";
print_r($req);

// Fill response
echo json_encode(array(
    \'html_link\' => \'\',\'base_string\' =>  $req->get_signature_base_string(),\'response\' => send_signed_request(\'GET\',array($req->to_header())),\'authorization_header\' => $req->to_header()
));
因此,当此代码运行时,将输出以下内容
OAuthRequest Object
(
    [parameters:OAuthRequest:private] => Array
        (
            [oauth_version] => 1.0
            [oauth_nonce] => 5f....11
            [oauth_timestamp] => 1306433873
            [oauth_consumer_key] => my.url.net
            [scope] => https://www.google.com/analytics/Feeds/
            [oauth_callback] => http://my.url.net/pete/oauth/
        )

    [http_method:OAuthRequest:private] => GET
    [http_url:OAuthRequest:private] => /accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fwww.google.com%2Fanalytics%2FFeeds%2F
    [base_string] => 
)
当我取消注释此行时:
//$req->sign_request($sig_method,NULL);
该脚本只是无法加载,显示服务器错误。我究竟做错了什么?任何建议都会有所帮助,谢谢!     

解决方法

感谢您的评论Mark,这实际上是因为我的\“ $ sig_method \”变量实际上只是一个包含\“ HMAC ... \”的字符串,而实际上它需要是一个名为this的对象:
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
希望这对以后的人有所帮助!