1

我编写了一个脚本,使用Net::OAuth对 Google 的纬度 OAuth API 进行身份验证。它正确地进行了身份验证(因为我可以成功地从 API 中获取数据)。但是,当我尝试添加历史条目时,我得到了401 Unknown authorization header响应。我正在使用以下代码:

my $location_data = $json->encode(\%data);

$request = Net::OAuth->request("protected resource")->new(
    consumer_key => $c_key,
    consumer_secret => $c_secret,
    token => $token,
    token_secret => $token_secret,
    verifier => $verifier,
    request_url => 'https://www.googleapis.com/latitude/v1/location',
    request_method => 'POST',
    signature_method => $s_method,
    timestamp => time,
    nonce => &nonce(),
    extra_params => {
        key => $s_key
    }
);

$request->sign;

$ua->default_header("Authorization", $request->to_authorization_header);
$ua->default_header("Content-Type", "application/json");

my $res = $ua->post('https://www.googleapis.com/latitude/v1/location?key=' . $s_key,
    Content => $location_data);

所有变量都在 API 的 fetch 部分中使用,所以我知道这些都可以。我不确定我是否使用了正确的 URL 来发帖,并且我已经尝试了上面示例中的内容以及$request->to_url.

任何建议将不胜感激。

4

1 回答 1

0

在与 Latitude API 团队反复讨论后,确定此错误来自Content-Type实际未设置为application/json. 将上面的代码更改为:

$ua->default_header("Authorization", $request->to_authorization_header);

my $res = $ua->post('https://www.googleapis.com/latitude/v1/location?key=' . $s_key,
    'Content-Type' => 'application/json',
    Content => $location_data);

一切都按预期工作。

于 2010-12-16T21:40:02.227 回答