Google OAuth 简单入门与使用
shiny 写于 2009, November 24, 6:29 PM. 微风日志
MeetTea需要能够通过Google API来获取用户的用户名和头像,效果就像google.org.cn的排队系统。
那么如何实现呢? 通过几天的学习、研究,下载google的源码,最后了解了原理并制作了一个简单的DEMO。
效果见附件,只有一个简单的test.php,需要自己去申请CONSUMER_KEY和CONSUMER_SECRET,不过申请步骤简单。
申请地址:herehttps://www.google.com/accounts/ManageDomains
目前仅使用HMAC-SHA1算法。此外可以参考这个http://googlecodesamples.com/oauth_playground/ 有源码,是PHP的。
详细的技术文档在这里 http://code.google.com/apis/accounts/docs/OAuth_ref.html
OAuth验证第一步:Get a Request Token
这个Request Token是请求一个令牌,来允许程序接入google的服务。
具体做法是发送一个HTTP GET请求到https://www.google.com/accounts/OAuthGetRequestToken
并且在HTTP头里有一个Authorization字段,字段里的内容是OAuth开头,加一个空格,然后是每个参数join一个“,”和空格。
此外google的OAuth还有一个SCOPE参数,这个不是标准OAuth里的。
最后注意端口是443,不然会收到一个302;如果程序有问题会收到一个400。
如果是200,则在返回的HTTP BODY里取得$oauth_token,$oauth_token_secret
保存好这两个值,下两步要用到。
第二步是用户操作部分,需要用户授权应用访问GOOGLE相关服务。也就是redirect到 www.google.com/accounts/OAuthAuthorizeToken?oauth_token=刚才的KEY
获取用户的头像、用户名的scope是http://www-opensocial.googleusercontent.com/api/people/@me
用户允许Access后就会跳转回刚才的页面,并且在URL参数里多了一个参数。
第三步是凭刚才URL里验证的参数发送一个GET请求到https://www.google.com/accounts/OAuthGetAccessToken
获取接入GOOGLE服务的权限,HEADER里仍旧要包含Authorization字段。
得到KEY和SECRET后替换刚才一开始的KEY和SECRET,发送GET请求到 获取用户的头像、用户名的scope,就可以保存下来,用户也无需注册了。
当然,GOOGLE庞大的API还可以帮助你得到用户朋友的EMAIL地址之类的,就看网站的需要了,潜力无限。
豆瓣用的也是OAuth。
==============2009/11/25 update=====================
豆瓣的算法和google的略有不同,主要是在http header里的数据不用rawurlencode
请搜索test.php里
PHP代码
- public function to_header($realm='') {
- $out ='Authorization: OAuth realm="' . $realm . '"';
- $total = array();
-
-
-
-
-
-
-
-
- foreach ($this->parameters as $k => $v) {
- if (substr($k, 0, 5) != "oauth") continue;
-
- $out .= ',' . $k . '="' . $v . '"';
- $str = $k . '=' . $v;
- if ($this->string == null) {
- $this->string = $str;
- } else {
- $this->string .= '&' . $str;
- }
- }
此外把params数组里的scope清理掉,豆瓣不需要。详细可以参考http://www.douban.com/service/apidoc/auth
其他基本是大同小异。豆瓣有PHP版的OAuth示例,可惜是做成ZEND FRAMEWORK的。我需要的是单个文件以便拆解到我的框架中。
接下来研究windows live的API和renren.com的API。
本文采用Creative Commons协议,复制本文需遵守三点:1、保留署名(链接);2、非商业性使用;3、再次创作的作品必须以相同的许可协议发布。法律顾问:庄毅雄律师
附件: test.7z (9.1 K, 下载次数:87)
Tags: oauth
« 上一篇 | 下一篇 »
rockdai
你好,我在基于你的代码通过豆瓣的OAuth的过程中遇到了一点问题。希望请教一下你。
可以加我QQ好吗? 5643593
谢谢
Post on 2009, December 15, 10:33 PM 1
shiny
不好意思,此外最后还有一点注意事项没贴出来,在豆瓣的常见问题里写了:签名不匹配的原因,和google有所区别:
关于用户签名的url
签名时需要用到将要访问的url。例如:
http://api.douban.com/people/sakinijino/collection
豆瓣OAuth认证,要求对这个url进行转义再计算签名,例如:
http://api.douban.com/people/@me
需要被转义为
http://api.douban.com/people/%40me
再计算签名。某些OAuth库默认不对url进行转义,你需要在传入url之前手工进行转义。这是签名不匹配的另一个原因。
具体源码有需要的可以向我索要,豆瓣/GOOGLE已经通过测试。
Post on 2009, December 16, 10:32 AM 2
rockdai
很感谢啊,如果你能开放源码给我的话就最好了。
我没找到你的联系方式,我把我的联系方式留下,你方便的话发给我好吗?谢谢了!
QQ: 5643593
邮箱: rockdai(AT)qq.com
Post on 2009, December 22, 12:03 AM 3
shiny
已发送。
Post on 2009, December 22, 9:41 AM 4
rockdai
谢谢,不过貌似我没有收到诶。
可以麻烦你再发一次么?或者发到
rockdai321@gmail.com
这个邮箱。
不好意思,麻烦你啦
Post on 2009, December 23, 12:40 PM 5
YaoFur
看着黄丝带好亲切的说...
我遇到的问题是GET正常..但是POST出错<douban>
签名不匹配...
我再好好研究下你的代码...
Post on 2010, January 4, 5:25 AM 6
xiaokai
为啥我就是不成功呢?
求源码. 601200376@qq.com
Post on 2010, April 14, 10:58 AM 7
rockcctv
哥们~发个豆瓣的代码吧~多谢
admin@rockcctv.com
Post on 2010, June 27, 1:34 AM 8
shiny
@rockcctv 已经大半年过去了,不知道代码还有用不。
Post on 2010, June 29, 9:22 PM 9
liushan
scope 参数的可选值都有什么,我在GOOGLE上没翻到,提供下可否,谢了~
mail:362724880@qq.com
Post on 2010, September 3, 2:35 PM 10
shiny
@liushan 注意Google技术文档里的说法:(required) URL identifying the service(s) to be accessed. The resulting token enables access to the specified service(s) only. Scopes are defined by each Google service; see the service's documentation for the correct value. To specify more than one scope, list each one separated with a space. This parameter is not defined in the OAuth standards; it is a Google-specific parameter.
再看这个
Determining the scope of the data to which you require access
Each Google service sets limits on the access it will allow through the Google Data APIs. This access is expressed as a scope value. Some services provide a variety of scope values, to allow a user to choose which applications should have access to which data. For information about the available scope values for the Google service you want to access, see the documentation for that service.
You should generally request a token for the broadest scope you expect to need. For example, if your application requires access to a user's Google Calendar feed, you should usually request a token for the scope http://www.google.com/calendar/feeds/, which grants access to all of Calendar's feeds.
If you request a token for the scope http://www.google.com/calendar/feeds/default/allcalendars/full, your application will have access to the user's "All Calendars" feed only, and will not have access to the feeds for individual calendars.
好,那什么是Google Data API呢?答案在这里http://code.google.com/apis/gdata/docs/directory.html
需要什么服务就翻什么吧。里面都写明了调用的URL,然后在你需要的服务URL放在scope参数,以空格隔开即可。
Post on 2010, September 4, 8:19 AM 11