7

我在我的Rails 3应用中使用了devise和omniauth-facebook进行Facebook身份验证,基于本教程:Adding Facebook auth to Rails 3.1 app,它的工作非常棒!从devise + omniauth-facebook身份验证访问令牌在fb-graph中使用

但是现在我想在我的应用程序中使用完整的Facebook集成功能,通过它我可以访问用户的照片,朋友等,为此我正在考虑使用fb_graph。 fb_graph需要一个标记,并且我想知道如何编辑我的用户模型以保存标记并在fb_graph中使用它。任何关于此事的帮助将不胜感激。

这是我的用户模型看起来像现在:

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 
+0

让我们参考,以创建注释[见这里](http://facebook-developer.net/2008/08/ 05 /优化,你的数据库,表换Facebook的连接/)。 创建'Facebook'模型并存储在该表中,但我不确定这是最好的方法。 :P – Rafaiel 2012-04-10 08:32:17

回答

9

你可以这样做:

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

您还需要添加:authentication_token或任何你把它命名为attr_accessible

+0

非常感谢!有效。你是一个拯救生命的人。 – humairatasnim 2012-04-11 17:05:57

+1

我现在面临的唯一问题是令牌过期。我为了测试目的从我的Facebook取消了应用程序的授权,现在当我尝试重新授权我自己时,出现此错误:'OAuthException ::必须使用活动访问令牌来查询有关当前用户的信息' 有没有任何方法每次用户登录到我的应用程序时都可以刷新令牌,以避免令牌过期? – humairatasnim 2012-04-11 17:19:45

+0

也许在调用self.authentication_token = nil的用户模型中创建一个方法; self.save;在deauth期间。不知道如何处理这个呢。几天后,我可能会。 – 2012-04-12 10:32:11

3

我在找同样的东西。我试图执行James Robey {s的答案,并感觉到相同的错误:OAuthException :: An active access token must be used to query information about the current user。然后我意识到authentication_token没有被这样保存。在看了一段时间后,我发现在FbGraph + OmniAuth + Facebook Graph API on Rails application中使用会话变量执行此操作的方法是,在omniauth回调期间实现令牌保存,然后在调用fb_graph时使用它。因此,它可能是这样的:

omniauth回调(应用程序/控制器/用户/ omniauth_callbacks_controller.rb)

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

    if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" 
# this part is where token is stored 
     auth = request.env['omniauth.auth'] 
     token = auth['credentials']['token'] 
     session[:fb_access_token] = token 
# 
     sign_in_and_redirect @user, :event => :authentication 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
end 

然后打电话时fb_graph

@fb_user = FbGraph::User.new('me', access_token: session[:fb_access_token]).fetch 

我不知道这是否是最好的方式,但迄今为止工作。

+0

谢谢你的回答。我还有一个问题 - 上面的句柄如何刷新标记? – BKSpurgeon 2017-04-22 00:49:19

1

病是指你https://github.com/nov/fb_graph/wiki/Page-Management

其中十一月声明:您所需要的页面的访问令牌来管理你的页面。 您的(用户)访问令牌在此处不起作用。

按照他的链接到Facebook的开发者网站获取更多信息

你需要的用户ACCESS_TOKEN和我这样做,通过omniauth-facbook https://github.com/mkdynamic/omniauth-facebook

,你先输入该初始化到检索使用您的应用程序

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], 
      :scope => 'email,user_birthday,read_stream', :display => 'popup' 
end 

(注意,您可以更改范围的权限在这里查看Facebook开发登录权限更

有一些内部运作omniauth和omniauth Facebook的,但JIST是 当你从Facebook获取回调你有一个机架omniauth哈希从请求

omniauth = request.env["omniauth.auth"] 

从已经可以访问该用户的访问令牌这样

facebook_user_token = omniauth['credentials']['token'] 

你就可以养活该令牌到您的fb_graph页面调用

page = FbGraph::Page.new('FbGraph').fetch(
    :access_token => facebook_user_token, 
    :fields => :access_token 
) 

那么你可以简单的可以通过调用所需方法,无需在访问令牌

note = page.note!(:subject => @title, :message => @message, :link => @url) 
相关问题