Devise + OmniAuth for Facebook注册远程服务器

时间:2014-04-06 18:34:23

标签: ruby-on-rails facebook devise omniauth

我正在使用Devise + Omniauth在我的应用程序中启用Facebook注册。当我开发它时,我没有遇到任何问题。与将其部署到我的远程服务器相同。问题是,其他人一直遇到同样的错误:

TypeError (no implicit conversion of Symbol into Integer):
app/models/user.rb:67:in `find_for_facebook_oauth'
app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'

我有用户模型user.rb的以下代码:

def self.find_for_facebook_oauth( data, signed_in_resource=nil)
user = User.where(:email => data.info.email).first
unless user
  params =
    {  
      :user =>
      {
        :username => data.uid,
        :email => data.info.email,
        :password => Devise.friendly_token[0,20],
        :user_profile_attributes => 
          {
            :first_name => data.extra.raw_info.first_name,
            :last_name => data.extra.raw_info.last_name,
            :remote_image_url => data.extra.raw_info.image,
          },
        :user_auths_attributes =>
        {
          :uid => data.uid,
          :provider => data.provider
        }
      }
    }
    user = User.create!(params[:user])
end
return user
end

第67行是user = User.create!(params[:user])

omniauth_callbacks_controller.rb

def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"])

if @user.persisted?
  sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
end
end

第4行是@user = User.find_for_facebook_oauth(request.env["omniauth.auth"])

服务器日志还显示GET参数:

Parameters: {"code"=>"[some long string of number and letters]", "state"=>"[another string of numbers and letters]"}

更新: 记录器为request.env["omniauth.auth"]输出以下内容:

#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1401992074 token="*"> extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash email="*" first_name="*" gender="male" id="*" last_name="*" link="https://www.facebook.com/*" locale="en_US" name="*" timezone=8 updated_time="2014-04-05T09:29:22+0000" username="*" verified=true>> info=#<OmniAuth::AuthHash::InfoHash email="*" first_name="*" image="http://graph.facebook.com/*/picture?type=square" last_name="*" name="*" nickname="*" urls=#<OmniAuth::AuthHash Facebook="https://www.facebook.com/*"> verified=true> provider="facebook" uid="*">

更新2: 记录params [:user]提供以下值:

Params: {:username=>"*", :email=>"*", :password=>"iePVLt7XEWk4YwPjja6n", :user_profile_attributes=>{:first_name=>"*", :last_name=>"*", :remote_image_url=>"http://graph.facebook.com/*/picture?type=square"}, :user_auths_attributes=>{:uid=>"*", :provider=>"facebook"}}

2 个答案:

答案 0 :(得分:0)

我发现只有这个问题:

:remote_image_url => data.extra.raw_info.image # In data I see only data.info.image

替换为

:remote_image_url => data.info.image

但它不是你问题的解决方案。

尝试从params[:user]调试数据。从异常看起来你在属性上使用了一些Hash。

答案 1 :(得分:0)

更新您的params哈希,如下所示:

params =
    {  
      :user =>
      {
        :username => data.uid,
        :email => data.info.email,
        :password => Devise.friendly_token[0,20],
        :user_profile_attributes => 
          {
            :first_name => data.extra.raw_info.first_name,
            :last_name => data.extra.raw_info.last_name,
            :remote_image_url => data.info.image ## Removed comma and updated the method
          },
        :user_auths_attributes =>
        [{
          :uid => data.uid,
          :provider => data.provider
        }] ## Enclosed within array [] brackets
      }
    }

看看你给出的params哈希,我可以说UserProfile1-1 RelationshipUserAuths有{ {1}}。在这种情况下,1-M Relationship必须作为user_auths_attributes传递。

Array

您收到了上述错误,因为TypeError (no implicit conversion of Symbol into Integer)解释为数组而不是哈希。所以当Ruby看到params [:user] [:user_auths_attributes] [:uid]它试图取最后一个键并将其转换为params [:user] [:user_auths_attributes] [0]或者至少找到一些可以转换为索引数组的整数值。