从devise + omniauth-facebook身份验证访问令牌,以便在fb-graph中使用

hum*_*nim 7 devise facebook-graph-api omniauth ruby-on-rails-3.1 fb-graph

我在我的Rails 3应用程序中使用了devise和omniauth-facebook进行Facebook身份验证,基于本教程:将Facebook身份验证添加到Rails 3.1应用程序,它运行良好!

但现在我希望在我的应用程序中有完整的Facebook集成,我可以使用它来访问用户的照片,朋友等,为此我正在考虑使用fb_graph.fb_graph需要一个令牌,我想知道如何编辑我的用户模型以保存令牌并在fb_graph中使用它.任何有关此事的帮助将受到高度赞赏.

这就是我的User模型现在的样子:

class User < ActiveRecord::Base

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable

# Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :photos
  has_many :scrapbooks

  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.create!(:email => data.email, :password => Devise.friendly_token[0,20])
    end  
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"]
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

Jam*_*bey 9

你可以这样做:

User.create!(:email => data.email, :password => Devise.friendly_token[0,20], :authentication_token => access_token.credentials.token)
Run Code Online (Sandbox Code Playgroud)

您还需要添加:authentication_token或您将其命名为attr_accessible的任何内容

  • 如果您有多个身份验证提供程序(例如facebook和twitter),是否有人知道如何刷新访问令牌?示例:用户始终使用Twitter登录,但他/她也有一个连接的Facebook帐户.在60天后,facebook令牌到期.那我怎么能同时刷新所有access_tokens呢? (2认同)