rails 集成 OAuth 第三方登录

Posted by wxianfeng Fri, 26 Oct 2012 18:04:00 GMT

最近需要为一个站集成 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!!!

This entry was posted on Fri, 26 Oct 2012 18:04:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , , , , ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=244

Comments

  1. Avatar
    greece ferry about {{count}} years ago:

    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

  2. Avatar
    Passenger Ships Bari Patras about {{count}} years ago:

    Quality articles is the secret to attract thhe viewers to visit
    the web site, that’s what this site is providing.

  3. Avatar
    Passenger Ships Bari Patras about {{count}} years ago:

    Quality articles is the secret to attract the viewers to visit the web site, that’s what this
    site is providing.

  4. Avatar
    Italygreece.Wordpress.com about {{count}} years ago:

    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!!

  5. Avatar
    Italygreece.Wordpress.com about {{count}} years ago:

    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!!

  6. Avatar
    greece ferry about {{count}} years ago:

    Excellent way of explaining, aand pleasant post to take data abut my presentation subject matter, which i am going to present
    in academy.

  7. Avatar
    greece ferry about {{count}} years ago:

    Excellent way of explaining, and pleaswnt post to take data about my presentation subject matter, which i am going
    to present in academy.

Leave a comment