2011-09-08 26 views
1

我試圖在保存該用戶時保存Facebook用戶的名稱,但我似乎無法做到。我跟着guides on the devise github和Facebook的整合工作正常;用戶郵件按原樣保存。但是,我無法弄清楚如何保存用戶名。現在,我這樣做:如何使用omniauth和設計保存Facebook名稱

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token['extra']['user_hash'] 
    if user = User.find_by_email(data["email"]) 
     user 
    else # Create a user with a stub password. 
     User.create(:email => data["email"], :name => data["name"], :password => Devise.friendly_token[0,20]) 
    end 
    end 

但這並不起作用。難道我做錯了什麼?

回答

4

好像驗證哈希模式已經發生了很大變化: https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token.extra.raw_info 
    if user = User.where(:email => data.email).first 
    user 
    else 
    # Create a user with a stub password. 
    user = User.create!(:username => data.username ? data.username : data.nickname , :email => data.email, :password => Devise.friendly_token[0,20]) 
    end 
end 
+0

不幸的是,這不起作用了(截至2013年1月) – colllin

0

的用戶名稱存儲在:

auth = request.env['omniauth.auth'] #I think this is what your access_token variable equates to. 
auth['user_info']['name'] 

如果這不是你所需要的,我建議你檢查的的access_token的內容。

+0

不幸的是,沒有。請求不能從該方法中訪問,並且我在[this stack overflow post]中找到(http://stackoverflow.com/questions/3580557/rails-3-using-devise-how-to-allow-someone-to -log-in-using-their-facebook-account),其中一個使用'data [「name」]給出的例子,並且查看Facebook的文檔,「name」應該在'[「extra」] [ 「user_hash」]' –