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

  21. Avatar
    Dane about {{count}} years ago:

    It’s really a great and helpful piece of information. I am satisfied that you just shared this useful info with us.
    Please keep us informed like this. Thank you for sharing.

  22. Avatar
    Dane about {{count}} years ago:

    It’s really a great and helpful piece off information. I am
    satisfied thyat you just shared this useful info with us.
    Please keep us informed like this. Thank yoou for sharing.

  23. Avatar
    Italy - Greece about {{count}} years ago:

    Please let mee know if you’re looking for a article writer for your blog.
    You have some really great articles and I believe I would be a
    good asset. If you ever want to take some of the load off, I’d really like to write some material
    for your blog in exchange for a ink back to
    mine. Please send me an email iif interested. Thank you!

  24. Avatar
    Italy - Greece about {{count}} years ago:

    Please llet me know if you’re looking for a article writer for your
    blog. You hafe some really great articles and I believe I would be a gopd asset.
    If you ever want to take some of the load off, I’d really lke to write some material for your blog in exchange for a link back to mine.

    Please send me aan email iif interested. Thank you!

  25. Avatar
    ipx.cc about {{count}} years ago:

    You’ve made some good points there. I checked on the
    inernet for more info about thhe issue and found most individuals will go along with your views on this site.

  26. Avatar
    ipx.cc about {{count}} years ago:

    You’ve ade some good oints there. I checked onn the internet foor more info abot the issue and found most individuals will
    go along with your views on this site.

  27. Avatar
    demteam.ru about {{count}} years ago:

    Wow, incredible blog structure! How lengyhy have you ever been ruunning a blog for?
    you made blogging look easy. The total look of your sitee is excellent, let alone tthe content material!

  28. Avatar
    dispora.sumselprov.go.id about {{count}} years ago:

    I have been exploiring for a litttle bit for any high-quality articles
    or weblog posts in this sort of areea . Exploring in Yahoo I eventually
    stumbled upon this web site. Reading this information So
    i’m glad to exhibit that I’ve a very just right uncanny feeling I
    came upon just what I needed. I such a lot without a doubt will mwke certain to don?t forgt this
    website and give it a look regularly.

  29. Avatar
    dispora.sumselprov.go.id about {{count}} years ago:

    I have beeen exploring for a little bit for any high-quality articles
    or weblopg posts iin this sort of area .
    Exploring in Yahoo I eventually stumgled upon this web site.

    Reading this information So i’m glad to exhibit that I’ve a very just right uncanny feeling I came upon just what I needed.
    I such a lot without a doubt will ake certain too don?t
    forget this website and give it a look regularly.

  30. Avatar
    Φθηνά ferries about {{count}} years ago:

    Really when someone doesn’t bbe aware of then its up to other
    viewers that they will assist, so here it occurs.

  31. Avatar
    Φθηνά ferries about {{count}} years ago:

    Really when someone doesn’t be aware of then its up to other
    viewers that they will assist, so here it occurs.

  32. Avatar
    fthina ferries about {{count}} years ago:

    Please let me know if you’re looking for a author for your blog.
    You have some really good posts and I feel I would be
    a good asset. If you ever want to take some of the load off, I’d absolutey love to write some
    articles for your blog in exchange ffor a link back to mine.
    Please blast me ann e-mailif interested. Regards!

  33. Avatar
    fthina ferries about {{count}} years ago:

    Please let me know if you’re looking for a author
    for yor blog. You haave some really ggood posts and
    I feel I would be a good asset. If you ever want to take slme of the
    load off, I’d absolutely love too wrife some articles for your blog in exchhange for
    a link back to mine. Please blast me an e-mail if interested.
    Regards!

Leave a comment