如何从Facebook与omniauth获取时区? [英] How to get timezone from facebook with omniauth?

查看:137
本文介绍了如何从Facebook与omniauth获取时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

user.rb中的所有内容在注册后正确传递,但时区不同。我无法弄清楚为什么在尝试登录用户时出现这个错误

  2016-03-30T20:13:38.083469 + 00:00 app [web.1]:NoMethodError(未定义的方法`timezone ='for#< User:0x007fdd2976e338>):
2016-03-30T20: 13:38.083470 + 00:00 app [web.1]:app / models / user.rb:72:in`block in from_omniauth'
2016-03-30T20:13:38.083471 + 00:00 app [web .1]:app / models / user.rb:66:在tap
2016-03-30T20:13:38.083471 + 00:00 app [web.1]:app / models / user.rb: 66:在`from_omniauth'
2016-03-30T20:13:38.083472 + 00:00 app [web.1]:app / controllers / sessions_controller.rb:7:在`facebook'

user.rb

  def self.from_omniauth(auth)
#设置60天的auth令牌
oauth = Koala :: Facebook :: OAuth.new(1540371223342976229929,ee917abf2e8f1c98274cd323fa1234ebb1346f4)#假数字
new_access_info = oauth.exchange_access_token_info auth.c redentials.token

new_access_token = new_access_info [access_token]
new_access_expires_at = DateTime.now + new_access_info [expires] to_i.seconds

其中(provider :auth.provider,uid:auth.uid).first_or_initialize.tap do | user |
user.provider = auth.provider
user.image = auth.info.image
user.uid = auth.uid
user.name = auth.info.first_name
user.last_name = auth.info.last_name
user.timezone = auth.info.timezone
user.email = auth.info.email除非user.email.present?
user.oauth_token = new_access_token#auth.credentials.token< - 您的旧令牌。不再需要了
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.password =(0 ... 8).map {(65 + rand(26))。chr} .join
user.activated = true
user.save!
end
end

config / omniauth.rb

  Rails.application.config.middleware.use OmniAuth :: Builder do 
provider:facebook,1540324372976229929 ,ee917abf2e8423f1c98274cdfa234234ebb1346f4,{info_fields:'email,first_name,last_name,timezone'}
end

t.floattimezone Facebook文档说时区应该是 float

解决方案

p>它看起来像omniauth提供的auth哈希有一个不同于你正在使用的结构。尝试这样。

  user.timezone = auth.extra.raw_info.timezone 
/ pre>

这是哈希结构,可用于Facebook omniauth。


Everything in user.rb is passing correctly upon sign up except timezone. I can't figure out why I'm getting this error upon a user attempting to sign in:

2016-03-30T20:13:38.083469+00:00 app[web.1]:   NoMethodError (undefined method `timezone=' for #<User:0x007fdd2976e338>):
2016-03-30T20:13:38.083470+00:00 app[web.1]:   app/models/user.rb:72:in `block in from_omniauth'
2016-03-30T20:13:38.083471+00:00 app[web.1]:   app/models/user.rb:66:in `tap'
2016-03-30T20:13:38.083471+00:00 app[web.1]:   app/models/user.rb:66:in `from_omniauth'
2016-03-30T20:13:38.083472+00:00 app[web.1]:   app/controllers/sessions_controller.rb:7:in `facebook'

user.rb

  def self.from_omniauth(auth)
     # Sets 60 day auth token
     oauth = Koala::Facebook::OAuth.new("1540371223342976229929", "ee917abf2e8f1c98274cd323fa1234ebb1346f4") # Fake Numbers
     new_access_info = oauth.exchange_access_token_info auth.credentials.token

     new_access_token = new_access_info["access_token"]
     new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds

    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.image = auth.info.image
      user.uid = auth.uid
      user.name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.timezone = auth.info.timezone
      user.email = auth.info.email unless user.email.present?
      user.oauth_token = new_access_token # auth.credentials.token <- your old token. Not needed anymore.
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.password = (0...8).map { (65 + rand(26)).chr }.join
      user.activated = true
      user.save!
    end
  end

config/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "1540324372976229929", "ee917abf2e8423f1c98274cdfa234234ebb1346f4", {info_fields: 'email, first_name, last_name, timezone'}
end

t.float "timezone" Facebook docs says timezone should be float.

解决方案

It looks like the auth hash provided by omniauth has a bit of a different structure than what you are using. Try this.

user.timezone = auth.extra.raw_info.timezone

Here is the hash structure that is available for Facebook omniauth.

这篇关于如何从Facebook与omniauth获取时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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