2017-01-05 6 views
0

私はOmniauthでFacebookからユーザーアドレスを取得しようとしていますが、動作しませんでした。Rails Facebook Omniauthユーザーアドレスを取得

ログイン後にアップデートコールバックにアドレスを追加しました。

私がomniauthからアドレスを削除した場合、アプリはアドレスを更新しませんでした。

誰かが自分のアドレスを取得する方法を知っていますし、ログイン後にアプリが自分のアドレスを編集して更新しなかったのはなぜですか?エラーを発生させる以外 - この

user.update_attribute(:address) 

は何もしていませんので、あなたのコードは動作しません

感謝の

def omniauth_callback 
    auth_hash = request.env['omniauth.auth'] 



    user = User.find_by_uid(auth_hash[:uid]) 
    if user.nil? && auth_hash[:info][:email] 
     user = User.find_by_email(auth_hash[:info][:email]) 
    end 

    if user.nil? 
     email_domain = '' 
     email_domain = '@facebook.com' if auth_hash[:provider] == 'facebook' 
     user = User.new(email: auth_hash[:info][:email] || auth_hash[:info][:nickname] + email_domain, name: auth_hash[:info][:first_name] || '', surname: auth_hash[:info][:last_name] || '', gender: 'I') 

     user.password_digest = '' 
     user.save!(validate: false) 
    end 

    user.update_attribute(:login_at, Time.zone.now) 
    user.update_attribute(:address) 
    user.update_attribute(:neighborhood) 
    user.update_attribute(:postal_code) 
    user.update_attribute(:ip_address, request.remote_ip) 
    user.update_attribute(:provider, auth_hash[:provider]) 
    user.update_attribute(:uid, auth_hash[:uid]) 
    user.update_attribute(:oauth_token, auth_hash[:credentials][:token]) 
    user.update_attribute(:oauth_expires_at, Time.at(auth_hash[:credentials][:expires_at])) 

    cookies[:auth_token] = { value: user.oauth_token, expires: user.oauth_expires_at} 
    redirect_to root_url 
    end 
+0

[Ruby on Rails Omniauthのfacebookが電子メールを返すことはありません]の可能な複製(http://stackoverflow.com/questions/31954540/ruby-on-rails-omniauth-facebook-doesnt-return-email) – OneNeptune

+0

@OneNeptune 、いいえ。あなたの住所はあなたの住所ではないから – jjplack

+0

Facebookがメールを提供していない、またはあなたのコードが提供されているメールを保存していないという問題はありますか? – mysmallidea

答えて

0

一つの理由です。 You have to pass a value into update_attribute as well as specify the field

また、@mysmallideaが指摘するように、use updateにすることをお勧めします。これは、1つのデータベースアクションで複数のフィールドを更新できるようにするためです。

存在する場合、アドレスデータはauth_hashになります。だから私はあなたが最初にそのハッシュの構造を工夫することをお勧めします。開発環境では、次の行を追加します。

ログ/ development.logへ auth_hash出力電流意志
Rails.logger.info auth_hash.inspect 

。これを使用して、アドレスデータがハッシュのどこにあるかを判断します。

user.update address: auth_hash[:info][:address] 

しかし、このアドレスは、facebook oauthシステムから返されたデータには含まれていないことがあります。この場合、可能であれば、documentationに戻る必要があります。

関連する問題