Fork me on GitHub

2008/03/07

Recent entries from same category

  1. curlを使ってコマンドラインからGDataにログインし、picasaに画像をアップロードする Hatena
  2. GmailのIMAPは、実は既に来ている Hatena

はてな
昨日のcurlの場合と対してやってる事は変わらない。
perlならばcpanの「Net::Google::GData」とか使った方がいいかもしれない。
ただ、まだ「Net::Google::GData」は使ったこと無い。
でも↓くらいのほうが見通し良い。

昨日のcurlの場合と同様に、GoogleLoginに使用する「auth」を取得してサービスコード「cp」に一覧を要求している。
#!/usr/bin/perl

use strict;
use LWP::UserAgent;
use HTTP::Request::Common qw(GET POST);
use URI::Escape qw(uri_escape);
use XML::LibXML;
use Data::Dumper;

my $gdata_user='your-account@gmail.com';
my $gdata_pass='your-password';

my $url = 'https://www.google.com/accounts/ClientLogin';
my %postdata = (
    Email => $gdata_user,
    Passwd => $gdata_pass,
    accountType => 'GOOGLE',
    source => 'Google-Contact-Lister',
    service => 'cp',
);
my $req = POST($url, [%postdata]);
my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);

my $google_auth = (split(/\n/, $res->content))[2];
$google_auth =~ s!^Auth=!!;

$url = 'http://www.google.com/m8/feeds/contacts/' .
    uri_escape($gdata_user) . '/base';

$req = GET($url, [%postdata]);
$req->header(
    Authorization => "GoogleLogin auth=$google_auth",
);
$res = $ua->request($req);
my $x = XML::LibXML->new;
my $doc = $x->parse_string($res->content)->documentElement;
my @nodes = $doc->getElementsByTagName('entry');
for my $node (@nodes) {
    my @title = $node->getElementsByTagName('title')->[0]->getChildNodes();
    my $gd = $node->getElementsByTagName('gd:email')->[0];
    print $title[0]->toString() . "," . $gd->getAttribute('address') . "\n"
        if @title && $gd;
}
実行結果は、めちゃめちゃ非公開なものなので、出せません><
CSV形式で名前とメアドが一覧されます。

初めてのPerl 第5版 初めてのPerl 第5版
Randal L. Schwartz
オライリージャパン / ¥ 3,780 (2009-10-26)
 
発送可能時間:在庫あり。

Posted at 15:26 in web::google | WriteBacks (0) | Edit
Tagged as: gdata, google, perl
Bookmarks: このエントリーのtweets add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip | add to buzzurl add to buzzurl | add to fc2bookmark add to fc2bookmark | add to Yahoo Bookmark add to Yahoo Bookmark | add to Pookmark add to Pookmark

blog comments powered by Disqus
WriteBacks

TrackBack ping me at
Post a comment

writeback message: Ready to post a comment.