2011-05-26 170 views
0

我想获得OAuth与谷歌分析工作,但我没有任何运气。我的代码如下所示:OAuth谷歌分析身份验证与PHP

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, $oauth_token, 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', $url, 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, $consumer, $oauth_token, NULL); 

脚本只是加载失败显示服务器错误。我究竟做错了什么?任何建议将有所帮助,谢谢!

+1

推测“服务器错误”是“500内部服务器错误”。检查服务器的错误日志,了解导致它的具体细节。您在浏览器中看到的东西完全无用于诊断目的。 – 2011-05-26 18:37:44

回答

0

感谢您的评论马克,这实际上是因为我的“$ sig_method”变量实际上只是包含字符串“HMAC ......”当它实际上需要的是一个对象调用此:

$sig_method = new OAuthSignatureMethod_HMAC_SHA1(); 

希望这有助于未来的人!