最近需要为一个站集成 OAuth 协议 第三方站点账号登录, 目前实现了两个 sina weibo 和 qq, 说下大致步骤 和注意事项,关键用了rails里面经典的gem omniauth.
weibo:
1, 到 http://open.weibo.com 申请应用, 会给你 key和secret
2, 有一步会验证你的 webmaster 权限,按照说明来即可
3, 我使用的gem
gem "omniauth", '1.1.0' gem 'omniauth-weibo-oauth2', '0.2.0'
4, 为 omniauth 配置 weibo provider
config/initliazers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do provider :weibo, '1978113365', '82fa3e1654c905b5b545a16945ahjiyb' end
5, 为了便于调试,修改 hosts 把域名指向本地
/etc/hosts
127.0.0.1 www.abc.com
6, 配置 callback 路由
config/routes.rb
match "/auth/:provider/callback" => "sessions#auth"
7, 访问weibo
www.abc.com:3000/auth/weibo
注意这里不能用 localhost访问,不然会得到:
redirect_uri_mismatch
错误
还有测试的时候,需要到 open.weibo.com 中指定测试账号,这个也需要注意下.
8, 成功后你的callback action中会收到返回给的数据,接下来就是你的事情了,基本的 你可以这样看到
Rails.logger.debug request.env["omniauth.auth"]
QQ:
大致步骤和 weibo 一样,但是不同的是 qq 需要配置 回调地址
我在open qq中配置的回调地址是 abc.com
但是本地调试时, 访问abc.com:3000/auth/qq 总是得到
redirect uri is illegal(100010)!
错误
最后找到的原因是 不能用 3000 端口, 需要使用 80 端口, 最后用 nginx + unicorn 配置80端口访问 解决了.
qq 用的gem是
gem 'omniauth-qq', :git => 'git://github.com/blankyao/omniauth-qq.git'
Ok, That’s ALL!!!
Trackbacks
Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=244
I know this iif off topoc but I’m looking into starting
my owwn blog and was wondering what all is required
to get setup? I’m assuming having a blog like yours would cozt a pretty penny?
I’m not very web savvy so I’m nnot 100% positive. Any tips or
advice would be greatly appreciated. Kudos
Quality articles is the secret to attract thhe viewers to visit
the web site, that’s what this site is providing.
Quality articles is the secret to attract the viewers to visit the web site, that’s what this
site is providing.
Hi, i feel that i saw you visited my weblog so i got here to return the prefer?.I am ttrying to in finding things to improve my website!I gudss its good
enough to use some of your ideas!!
Hi, i feel that i saw you visited my weblog so i got here
to return the prefer?.I am trying to iin finding things
to improve my website!I gguess its goood enough too use some of your ideas!!
Excellent way of explaining, aand pleasant post to take data abut my presentation subject matter, which i am going to present
in academy.
Excellent way of explaining, and pleaswnt post to take data about my presentation subject matter, which i am going
to present in academy.
Hello, this weekend is fastidious in ffavor of me, as this time i am reading this wonderful
educational paragraph here at my house.
Hello, this weekend is fastidioujs in favor oof me, as this time
i am reading this wonderful educational paragraph here at my house.
Nice post. I used to be checking constantly this weblog and I am impressed!
Extremely helpful info particularly the last phase :) I maintain succh information much.
I waas sedking this particular info for a long time.
Thanks and good luck.
Nice post. I used to be checking constantly this weblog aand I am impressed!
Extremely helpful info particularly the ast phase :
) I maintain such information much. I was seeking this particular info for a longg time.
Thanks annd good luck.
I’m curikus to find out what blog platform you happen to be using?
I’m experiencing some sall security issues with my latest blog and I woyld like to find something more risk-free.
Do you have any suggestions?