
新年伊始,中文推特圈twitese作者向我发来了新年的问候:
bang590: @iamzzm 有兴趣把OAuth登陆加到原版twitese吗~
这实在是一件荣幸的事,但没信心直接参与原版,把代码和说明写在下面,作者可以自己加进去,或者有人写新的第三方时,应该可以参考一下。
oAuth需要翻墙,有什么用?
- 在第三方应用网站上,如果你觉得该网站不可靠,输入的用户密码也许会被该网站的拥有者记录窃取,那么使用oAuth就可以不用担心了:因为oAuth方式是通过直接登陆twitter官网,向第三方网站提供一个访问授权,而不是提供你的密码。
- 虽然需要翻墙登陆twitter点击一次“同意”,但只需翻一次,直到退出登录或关闭浏览器。
- 使用oAuth可以自定义推的来源为“通过xxxx”,而不是大众脸的“通过web”。
- 使用第三方应用的理由并不仅仅是“不用翻墙”。白名单恐怖之下,当电信局的拨号软件老是打不开网页时,翻墙软件将成为真正意义上的拨号软件。
2009是“推墙元年”,2010将由于白名单而成为“翻墙元年”,出于对twitter的热爱,我们除了免于翻墙的第三方,还可以做更多更好玩的第三方,在那堵墙上留下自己的涂鸦,就像当年留在柏林墙上的一样。
其实,我个人仅仅是为了能在推后面显示“通过真理部内参”才去研究oAuth的,基本过程就是把dabr的oAuth部分加到twitese中去,虽然大部分的代码我根本看不懂,但连猜带蒙不断试错善于google是我的特长,最后意外成功了,虽然只是一个菜鸟的成果,但权当新年礼物,送给有需要的人们:
config.php增加:
define(‘OAUTH_CONSUMER_KEY’, ”);
define(‘OAUTH_CONSUMER_SECRET’, ”);
define(‘OAUTH_CALLBACK_URL’, ”);
这里定义oAuth的相关密钥,登陆https://twitter.com/oauth,新建一个程序,填好保存即可获得OAUTH_CONSUMER_KEY和OAUTH_CONSUMER_SECRET的内容,其中Callback URL一项是OAUTH_CALLBACK_URL的内容,指验证后返回的网址,一般填你的第三方所在网址后面加上/oauth,如http://nc.alwaysdata.net/oauth。
为了搞懂/oauth什么意思花了我好多时间,原来这个叫“伪静态”,需要一个.htaccess文件,直接用dabr的就好了,里面有句RewriteRule ^(.*)$ index.php?q=$1 [L,QSA],意思大概是http://nc.alwaysdata.net/oauth其实就是http://nc.alwaysdata.net/index.php?q=oauth,所以
index.php增加:
<?php if(isset($_GET['q'])){if($_GET['q'] == ‘oauth’){user_oauth();}} ?>
(真理部内参是写在login.php中然后包含进index.php的)
这个user_oauth()是oAuth登陆主通道,也修改自dabr:
function user_oauth() {
//OAuth.php直接来自dabr,大概包括oAuth的主要函数,未改,也读不懂~
require_once ‘lib/OAuth.php’;
session_start();
if (isset($_GET['oauth_token'])) { //以下分支是验证后返回要做的事
$oauth_token = $_GET['oauth_token'];
$params = array(‘oauth_verifier’ = >$_GET['oauth_verifier']);
$response = oauth_process(‘https://twitter.com/oauth/access_token’, $params);
parse_str($response, $token);
//$GLOBALS['oauthpass'] 好像是一组验证码电子签名之类的
$GLOBALS['oauthpass'] = $token['oauth_token'].’|’.$token['oauth_token_secret'];
unset($_SESSION['oauth_request_token_secret']);
$user = oauth_process(‘https://twitter.com/account/verify_credentials.json’);
if (!isset($user – >error)) { //登陆成功写入相关cookie,当然twitterPW不是真正的密码
setEncryptCookie(‘twitterPW’, $GLOBALS['oauthpass']);
setEncryptCookie(‘twitterID’, $user – >screen_name);
setcookie(‘friends_count’, $user – >friends_count);
setcookie(‘statuses_count’, $user – >statuses_count);
setcookie(‘followers_count’, $user – >followers_count);
setcookie(‘imgurl’, $user – >profile_image_url);
setcookie(‘name’, $user – >name);
setCookie(“bOauth”, “oauth”); //标明现在是在用oAuth登陆
header(‘location: ../index.php’);
} else {
header(‘location: index.php?ln=error’);
}
exit();
} else { //以下分支是为开始验证而做的准备
$params = array(‘oauth_callback’ = >OAUTH_CALLBACK_URL);
$response = oauth_process(‘https://twitter.com/oauth/request_token’, $params);
if (($oauth_token = $_GET['oauth_token']) && $_SESSION['oauth_request_token_secret']) {
$oauth_token_secret = $_SESSION['oauth_request_token_secret'];
} else {
list($oauth_token, $oauth_token_secret) = explode(‘|’, $GLOBALS['oauthpass']);
}
$token = new OAuthConsumer($oauth_token, $oauth_token_secret);
parse_str($response, $token);
$_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];
$authorise_url = ‘https://twitter.com/oauth/authorize?oauth_token=’.$token['oauth_token'];
//带着oauth_token前往twitter.com要求验证
header(“Location: $authorise_url”);
}
}
//处理URL,跟原twitese中twitter.php的process()差不多
function oauth_process($url, $post_data = false) {略…}
//oAuth式登陆,基本看不懂,但改了一些
function user_oauth_sign( & $url, &$args = false) {略…}
twitter.php修改:
首先要在class中加一个判断是否在用oAuth:
var $IsoAuth = false;
然后通过twitter()判断$IsoAuth:
function twitter($username = ”, $password = ”, $BeoAuth = false)
{
if($BeoAuth){$this->IsoAuth = true;}
if ($username != ” && $password != ”) {
$this->username = $username;
$this->password = $password;
}
}
相应的,process()函数需要增加分支:
if($this->IsoAuth){
if ($postargs) $postargs = array();
$this->oauth_sign($url, $postargs);
$ch = curl_init($url);
if($postargs !== false) {
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
}else{略…}}
另增加一个function oauth_sign(&$url, &$args = false) {略…}
这时已经可以成功登陆获取数据,但发推出来的都是URL编码的字符,这我也不懂,dabr由于整个框架跟twitese大不相同也看不明白怎么解码,最后猜到用rawurldecode,在update、sendDirectMessage、updateProfile、createList、editList等函数中,return之前解码:
if($this->IsoAuth){$request = rawurldecode($request);}
twitese.php修改:
function getTwitter() {
return new twitter(getEncryptCookie(‘twitterID’), getEncryptCookie(‘twitterPW’), user_type());
}
function user_type() {//通过cookie判断是否在用oAuth,bOauth由登陆时的user_oauth()写入
if (isset($_COOKIE["bOauth"])) return true;
else return false;
}
退出登陆时记得delCookie(‘bOauth’);
大概就是这些了,但有个问题好像是由于URL编码字节比中文多,造成中文字数受到更多限制,原dabr也存在这个问题,未解决~
另外,上传图片功能需要向图片网站提供密码,oAuth没密码所以没法上传——也许可以在用户想上传图片时要求输入一次密码。
真理部内参源代码: 说明 下载 SVN
信息时代的闭关锁国正在开始,希望有关部门能多读点晚清历史借鉴一下,世事的转折点往往会突然出现,眼前的利益其实没有任何保证。2010,我将努力保持耐心、远离悲观,虽然无法像今天的香港同胞一样上街呐喊,但宅在家中,无论做的东西多么微小,也将继续做下去。