0

OAuth を Google アナリティクスで動作させようとしていますが、うまくいきません。私のコードは次のようになります。

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

スクリプトの読み込みに失敗し、サーバー エラーが表示されます。私は何を間違っていますか?どんなアドバイスも役に立ちます、ありがとう!

4

1 に答える 1

0

コメントマークに感謝します。これは、実際には「$ sig_method」変数が実際には「HMAC...」を含む文字列であり、実際には次のオブジェクトである必要があるためです。

$sig_method = new OAuthSignatureMethod_HMAC_SHA1();

これが将来誰かに役立つことを願っています!

于 2011-05-26T21:48:24.563 に答える