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.

  8. Avatar
    nex.tf about {{count}} years ago:

    Hello, this weekend is fastidious in ffavor of me, as this time i am reading this wonderful
    educational paragraph here at my house.

  9. Avatar
    nex.tf about {{count}} years ago:

    Hello, this weekend is fastidioujs in favor oof me, as this time
    i am reading this wonderful educational paragraph here at my house.

  10. Avatar
    Bari - Patras about {{count}} years ago:

    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.

  11. Avatar
    Bari - Patras about {{count}} years ago:

    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.

  12. Avatar
    k1news.ru over {{count}} years ago:

    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?

  13. Avatar
    passenger Ships Bari patras over {{count}} years ago:

    It’s awesome to pay a visit this web site and reading the views of all mates
    on the topic of this piece of writing, while I am also eager off getting
    know-how.

  14. Avatar
    passenger Ships Bari patras over {{count}} years ago:

    It’s awesome to pay a visit this web site and reading
    the views of all mates on the topic of his piece of writing,
    while I am also eager of getting know-how.

  15. Avatar
    Grimaldi lines almost {{count}} years ago:

    Excellent site you havce here but I was wondering if you knew of any message boards that covcer the
    same topics discussed here? I’d really like to be a part of online community where I can get advice from other
    experienced individuals that share the same interest.
    If you have any suggestions, please let me know. Thank you!

  16. Avatar
    Grimaldi lines almost {{count}} years ago:

    Excellent sute you have here but I was wondering if you knew of any message boards that cover the same tpics discussed here?
    I’d really likee to be a part of online community where I can get
    advice fom other experienced individuals that share the same
    interest. If you have any suggestions, please let mee
    know. Thak you!

  17. Avatar
    Passenger Ships Bari Patras almost {{count}} years ago:

    Having read this I thought it was rather enlightening.
    I appreciate you spending some time and energy to put this short articke together.
    I once again find myself personally spending a significant ammount of
    time bkth reading and commenting. But so what, it waas still worthwhile!

  18. Avatar
    Passenger Ships Bari Patras almost {{count}} years ago:

    Having read this I thought it was rather enlightening.
    I appreciate you spending some time and energy to put thjis short article together.

    I once again ffind myself personally spending a significant
    amount of time both reading and commenting. But so what, it was still worthwhile!

  19. Avatar
    Tisha almost {{count}} years ago:

    It’s awesoe designed for me to hhave a web site, which is useful in support of
    my experience. thanks admin

  20. Avatar
    Tisha almost {{count}} years ago:

    It’s awesome desikgned for me to have a web site, which is useful in support of my experience.
    thanks admin

Leave a comment