Saturday, July 18, 2009

to Get Google Reader Token codes

想写一个脚本把Google reader上我订阅的所有Feed里的图片抓下来(最近订了几个毛图站,哈哈)。看了Google Reader API,挺简单。但比较讨厌的是如果要设置一个read-listing里的item为已读,则要用到token。在网上找了一圈没有。。妈的,只好自己写。这个token不只是要Google登陆验证,有两个步骤,请看代码:


/* ------- start code ------- */
$authentication_url = 'https://www.google.com/accounts/ClientLogin';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authentication_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "service=reader&Email=[your google account]&Passwd=[your pw]");
ob_start();
curl_exec($ch);
$sid = ob_get_clean();
curl_close($ch);

$cookie = preg_replace('/[\r\n]/','; ',$sid);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/reader/api/0/token');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
ob_start();
curl_exec($ch);
$token = ob_get_clean();
curl_close($ch);

echo $token //here you go baby.
/* ------- end code ------- */

No comments:

Post a Comment