对gtap的一些wordaround
February 28th, 2010 | by vvoody |前两天发现gtap这个项目,也是个Twitter API代理,基于GAE(Python)。想着Choqok、qwit都没法用birdnest,所以架了个玩玩。结果弄出一堆事来 =.=!
先是把gtap和其它几个app放在一起,如我上篇文章所讲的。这个挺简单:
diff --git a/gtap.py b/gtap.py index 1b9d8ed..472157d 100644 --- a/gtap.py +++ b/gtap.py @@ -16,6 +16,7 @@ class MainPage(webapp.RequestHandler): origUrl = self.request.url origBody = self.request.body (scm, netloc, path, params, query, _) = urlparse.urlparse(origUrl) + path = path[5:] # skip '/gtap' if path == '/': self.myOutput('text/html', 'here is the proxy of \"twitter.com\" by GTAP %s !' % (gtapVersion)) else: @@ -59,7 +60,7 @@ class MainPage(webapp.RequestHandler): self.doProxy('get') def main(): - application = webapp.WSGIApplication( [(r'/.*', MainPage)], debug=True) + application = webapp.WSGIApplication( [(r'/gtap/.*', MainPage)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == "__main__":
在app.xml里加上:
- url: /gtap/.* script: gtap.py # secure: always
如果你把gtap单独架设在gae上的,而不是和几个app一起放在yourapp.appspot.com上的话,这步不需要。
接着,发现Choqok等客户端发出的用户身份验证请求是到api.twitter.com,但http://api.twitter.com/account/verify_credentials.xml这样请求无效,改成http://twitter.com/account/verify_credentials.xml即可:
diff --git a/gtap.py b/gtap.py index 472157d..f50f2dc 100644 --- a/gtap.py +++ b/gtap.py @@ -36,7 +39,7 @@ class MainPage(webapp.RequestHandler): netloc = 'search.twitter.com' newpath = path[7:] elif path_parts[1] == 'api': - netloc = 'api.twitter.com' + netloc = 'twitter.com' newpath = path[4:] else: netloc = 'twitter.com'
最后是最头疼的,本来经过上面两次改动echofon已经是可以正常收发推了。可偏偏Choqok和Qwit就是没法用gtap的api代理。老是提示”Could not authenticate you.“。经过抓包,我发现,这两个客户端竟然不会像其它客户端一样在http请求中加入Authorization头部,也就是用户名和密码,用来给网站验证的,难怪会出现错误。哪位朋友如果用Choqok/Qwit并且使用birdnest api代理的请支一声~ 经过一番摸索(省去纠结的zzzzzzzz过程),改动了gtap对http头部不含有Authorizatioin的处理:
diff --git a/gtap.py b/gtap.py index 472157d..f50f2dc 100644 --- a/gtap.py +++ b/gtap.py @@ -22,6 +22,9 @@ class MainPage(webapp.RequestHandler): else: if 'Authorization' not in self.request.headers : headers = {} + self.response.set_status(401, message="Unauthorized") + self.response.headers.add_header('WWW-Authenticate', 'Basic realm="Secure Area"') + self.response.out.write("401") else: auth_header = self.request.headers['Authorization'] auth_parts = auth_header.split(' ')
原理是如果客户端的http请求头部没有Authorization字段,那么gtap返回”401 Unauthorized”,让它重新发送用户验证信息。再次抓包,Choqok第一次还是没有Authorization,接着就收到服务器发来的401状态码,于是它乖乖的再次发送请求并包含Authorization。
这样改之后echofon等依然可以正常使用gtap的api代理。也许你有注意到我在app.xml里注释掉了使用安全链接”# secure: always”,经测试Choqok在http下可以正常处理收发tweets以及其它api操作,但改用https后,发推就不行了,其它还是可以的。Qwit因为貌似不支持https,所以只能用http的。
虽然经过这么workaround,Choqok、Qwit能看推发推了,但我还是觉得Choqok这个客户端自身有些问题,因为如果换用twip这个api代理的话,无论http、https都能正常使用,偏偏birdnest和gtap的不行。如果说gtap应用少的话,birdnest可是使用广泛的twitter api代理啊,我的Twitter Opera Widget就用的它。不想再折腾了,如果你也在用gtap,希望本文能给你些帮助
唉,连续两篇博文在讲twitter api代理的事儿了,老外上个网哪有天朝这么多的屁事,我朝屁民们在代理的事上浪费多少时间啊…
参考:
[1] 基本连接验证(Basic Access Authorization)的原理
[2] Status Code Definitions
[3] Authorization
[4] HTTP Request fields
[5] List of HTTP status codes
[6] Basic access authentication
UPDATE 2010-3-1
gtap的作者@rui7905非常迅速地更新到了0.3版,修正了我这篇文章提到的问题,并且做了原因分析,感兴趣得朋友移步讨论页。如果你用gtap,不要再用在下workaround的拙作了,升级到0.3吧~~
5 Responses to “对gtap的一些wordaround”
By Kars on Apr 10, 2010
Flock 2.5.6 Windows XP Flock 2.5.6 Windows XP学习了、、、
Reply
By vvoody on Apr 10, 2010
Mozilla Firefox 3.6.3 Linux Mozilla Firefox 3.6.3 Linux嘿嘿,还是看一下作者的解决方法吧
http://code.google.com/p/gtap/issues/detail?id=3
Reply
Kars Reply:
Flock 2.5.6 Windows XPApril 10th, 2010 at 13:30
嗯哪~ o(* ̄▽ ̄*)o~
Reply
By tv shows on Jul 15, 2010
Internet Explorer 7.0 Windows XP Internet Explorer 7.0 Windows XPExcellent post thank you!
Sent via Blackberry
Reply
By ghd on Jul 30, 2010
Mozilla Firefox 3.6.3 Windows XP Mozilla Firefox 3.6.3 Windows XPi believe you are a good writer, but have you erver thought to write some special rtcals for peopel who likes shopping very much wansantg2zxy.
Reply