我编写了一个脚本,可以使用Google的Oauth API进行鉴定,用于使用 net :: Oauth. 。它正确身份验证(因为我可以成功地从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的获取部分中使用,因此我知道这些都可以。我不确定我是否正在使用正确的URL来发布,并且我尝试了上面的示例中的内容,以及 $request->to_url.

任何建议将不胜感激。

有帮助吗?

解决方案

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

一切都按预期工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top