我可以将facebook id +令牌传递给Rails Devise服务来验证用户吗?

时间:2011-06-22 00:17:30

标签: ruby-on-rails facebook devise omniauth

我有一个运行Devise + OmniAuth的Rails Web服务。我让用户在移动设备上对Facebook Connect进行身份验证。然后,我希望移动设备与我们的Web服务建立会话。从移动客户端传递到设备以建立此会话需要什么?在Rails方面是否有一个代码示例如何处理从facebook传递的id +令牌 - >移动 - >网络服务?

2 个答案:

答案 0 :(得分:4)

我还没有找到一个好方法。我也没有使用:omniauthable,我正在单独使用OmniAuth和Devise,如Railscast情节中所示,其中有两个表usersauthentications。它有点哈哈,只适用于Facebook。

基本上,通过SSL或类似的方式将您的access_token从iPhone发送到服务器。您必须首先检查OmniAuth,如果您被授予访问权限,那么您可以通过以下方式手动创建与OmniAuth的会话:

我确实通过这样做得到了一些工作:

FB = OmniAuth::Strategies::Facebook.new("nothing") 

client = ::OAuth2::Client.new("nothing", "nothing", FB.client_options) 

cached_token = "app_id_part|session_id_part|token_part" # or maybe you sent it from the iPhone
access_token = ::OAuth2::AccessToken.new(client, cached_token)  

FB.instance_variable_set("@access_token", access_token) 

FB.auth_hash 
# You will either get a hash or get this error:
# OAuth2::AccessDenied: Received HTTP 401 during request.

之后,您可以通过Authentications表查找找到用户所需的用户信息:

@user = Authentication.where(:uid => FB.auth_hash["uid"], :provider => "facebook").first.user

现在我们创建一个会话:

sign_in_and_redirect(:user, @user)

# or, perhaps

sign_in(@user, :bypass => true)

答案 1 :(得分:2)

我很难用这个,所以我会在这里为其他人发布我的答案。这是受到Dex上面答案的启发。

FB = OmniAuth::Strategies::Facebook.new("APP_ID", "APP_SECRET") 
client = OAuth2::Client.new("APP_ID", "APP_SECRET", FB.options.client_options) 
token = OAuth2::AccessToken.new(client,'ACCESS_TOKEN', FB.options.access_token_options)
FB.access_token = token
FB.auth_hash

任何改善这一点的建议都将受到赞赏。

上面的代码段允许我通过使用access_token查询facebook来获取auth_hash

我正在使用这个gem:github.com/mkdynamic/omniauth-facebook