Search code examples
perloauthgoogle-latitude

Google Latitude and OAuth Signed requests


I've written a script that authenticates against Google's OAuth API for Latitude, using Net::OAuth. It correctly authenticates (as I can successfully fetch data out of the API). However, when I try to add an historical entry, I get a 401 Unknown authorization header response. I'm using the following code:

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

All of the variables are used in the fetch portion of the API, so I know those are all ok. I'm not sure if I'm using the correct URL to post against, and I've tried what's in the sample above, as well as $request->to_url.

Any suggestions would be greatly appreciated.


Solution

  • After some back and forth with the Latitude API team, it was determined that this error comes from the fact that the Content-Type is not actually being set to application/json. Changing the above code to:

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

    And everything works as expected.