Rails 中的 OAuth - 谷歌、推特、脸书、连接登录,如 stackoverflow 登录 [英] OAuth in Rails - google, twitter, facebook, connect for login like stackoverflow login

查看:46
本文介绍了Rails 中的 OAuth - 谷歌、推特、脸书、连接登录,如 stackoverflow 登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 有其余的 autho 插件,它运行良好,但是否有整合 twitter、facebook、google、yahoo 等的解决方案...

Rails has the rest autho plugin which works well but is there a solution for incorporating twitter, facebook, google, yahoo, etc...

似乎每个人都有自己的插件和需求,将它们混合起来会很麻烦.

Seems like each on has its own plugin and demands and mixing them is going to be a mess.

这是为了登录用户,例如 Stackoverflow 如何完成工作,而不是使用 API 的强大功能.

This is for logging in users like how Stackoverflow gets things done not for using the robust features of the APIs.

我想要做的是做 stackoverflow 为登录所做的事情,但在 Rails 中.

What I want to do is do what stackoverflow did for login but in rails.

推荐答案

编写自己的控制器代码来连接到这些服务中的每一个并进行重定向并不难.例如,使用 oauth 对 twitter 进行身份验证需要两个操作,总共大约 20 行代码.

It's not too difficult to write your own controller code to connect to each of these services and redirect. For example, to authenticate to twitter using oauth takes two actions and about 20 lines of code total.

将每个服务的代码单独保存在其自己的控制器中.

Keep the code for each service separate in it's own controller.

def twitter_oauth
  o = Twitter::OAuth.new(your_twitter_consumer_token, your_twitter_consumer_secret, :authorize_path => '/oauth/authenticate', :sign_in => true)
  o.set_callback_url(twitter_cb_url)
  session[:twitter_oauth_request_token] = o.request_token.token
  session[:twitter_oauth_request_secret] = o.request_token.secret
  redirect_to o.request_token.authorize_url
end

def twitter_oauth_cb
  o = Twitter::OAuth.new(your_twitter_consumer_token, your_twitter_consumer_secret, :authorize_path => '/oauth/authenticate', :sign_in => true)
  if params[:denied]
    redirect_to root_url
  elsif params[:oauth_verifier]
    o.authorize_from_request(session[:twitter_oauth_request_token], 
                             session[:twitter_oauth_request_secret],
                             params[:oauth_verifier])
    # look up this user in the db by o.access_token.token 
    # is the user not found? create them, save their token
    # log them in - UserSession.create(user, true)
    redirect_to root_url
  end
end

这篇关于Rails 中的 OAuth - 谷歌、推特、脸书、连接登录,如 stackoverflow 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆