V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
stranbird
V2EX  ›  Ruby on Rails

Rails里面如果要做一个Oauth过程,在求Authorize的时候会redirect。该怎么维护那个consumer对象?把token和secret存在session里面?

  •  
  •   stranbird · 2011-04-01 20:50:52 +08:00 · 5579 次点击
    这是一个创建于 4745 天前的主题,其中的信息可能已经有所发展或是发生改变。
    5 条回复    1970-01-01 08:00:00 +08:00
    icyflash
        1
    icyflash  
       2011-04-01 20:52:19 +08:00
    dreamer
        2
    dreamer  
       2011-04-01 20:56:37 +08:00
    真巧,我最近也在研究这个。

    你也可以不存到 session 里,在 Authorize redirect 的时候可以带上 oauth_callback 参数,把 token 和 secret 带上就行了,例如:

    http://xxx.com/authorize?oauth_token=xxxx&oauth_token_secret=xxx&oauth_callback=http://yourname.com/aaa?oauth_token=xxx&oauth_token_secret=xxx

    等授权通过之后,对方会跳转到 oauth_call_back 指定的网址,这样你就可以获取 http query string 了。
    chloerei
        3
    chloerei  
       2011-04-01 20:58:25 +08:00
    做过一次,觉得未校验成功时放session里面比较环保。校验成功了再存数据库里。

    另外可以看看这个 https://github.com/intridea/omniauth
    stranbird
        4
    stranbird  
    OP
       2011-04-01 20:58:47 +08:00
    @dreamer 好办法
    stranbird
        5
    stranbird  
    OP
       2011-04-01 21:05:00 +08:00
    还有个问题,是不是认证的方法一定要放在controller里面?
    但是每次都在controller里边放一个OAuth::Consumer.new感觉很奇怪。
    model里边可以放么?好像model里边没有redirect_to啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   983 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:59 · PVG 05:59 · LAX 14:59 · JFK 17:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.