Connecting to Google Reader from Perl

| 1 Comment
I've been thinking about redoing my news reader plugin for Movable Type based around Google Reader. In order to do that, I had to find out some things about the Google Reader API, at least as far as is publicly known about its API (if you can call it that). This blog post proved to be invaluable in getting this working sample (and understanding) up and running. It's ugly as hell, and I know there is a CPAN module for GoogleReader (that may or may not still be working), but I figured I'd learn how it works through trial and error in order to better understand it.

use strict;
use LWP;
use HTTP::Cookies;
use HTTP::Request;
use HTTP::Response;
use HTTP::Request::Common qw( GET POST );
use URI;
use URI::Escape;
use URI::QueryParam;

my $login = 'me@gmail.com';
my $password = '*******';
my $source = 'CMR';

my $google_url = 'http://www.google.com';
my $reader_url = $google_url . '/reader';
my $login_url = 'https://www.google.com/accounts/ClientLogin';
my $token_url = $reader_url . '/api/0/token';
my $subscription_list_url = $reader_url . '/api/0/subscription/list';
my $reading_url = $reader_url . '/atom/user/-/state/com.google/reading-list';
my $read_items_url = $reader_url . '/atom/user/-/state/com.google/read';
my $reading_tag_url = $reader_url . '/atom/user/-/label/%s';
my $starred_url = $reader_url . '/atom/user/-/state/com.google/starred';
my $subscription_url = $reader_url . '/api/0/subscription/edit';
my $get_feed_url = $reader_url . '/atom/feed/';

my $browser = LWP::UserAgent->new(agent => $source);
my $response = $browser->post($login_url, [
     'Email' => $login,
     'Passwd' => $password,
     'service' => 'reader',
     'continue' => $google_url,
     'source' => $source
]);

my $sidline = $response->{_content};
my $sid = substr($sidline, length("SID="), length($sidline));
my @parts = split(/\n/, $sidline);
$sid = substr($parts[0], length("SID="), length($parts[0]));
print "$sid\n";
unless ( $browser->cookie_jar ) {
     $browser->cookie_jar( HTTP::Cookies->new( hide_cookie2 => 1 ) );
}
$browser->cookie_jar->set_cookie(0, SID=>$sid, '/', '.google.com', undef, 1, 0, 160000000000);
my $uri = URI->new($subscription_list_url);
my $req = GET($uri);
$req->uri->query_param( ck => time * 1000);
$req->uri->query_param( client => $browser->agent);
$req->uri->query_form( { $uri->query_form, output => 'xml'} );
my $res = $browser->request($req);
if ($res->is_error()) { print "ERROR!!!!\n"; }
print $req->as_string() . "\n";
print $res->decoded_content() . "\n";

1 Comment

Hi Mike,

Thanks for the posting, got me through a couple of issues!

Regards,
Gareth

Leave a comment

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Recent Entries

The three purposes of the federal income tax law
Businesses will spend about 3.4 billion man-hours and individuals about 1.7 billion hours figuring out their taxes this year.…
Progress of a different sort
You know we have reached a level of decadence seldom seen in the history of the West when our women…
And police wonder why the public rarely trusts them
But there is some good news to report here, too. The Maryland state law, as noted, is the first…

Subscribe

Advertisements

OpenID accepted here Learn more about OpenID