应用程序中使用了 devise 和 omniauth-facebook 进行 Facebook 身份验证,基于本教程:将 Facebook auth 添加到 Rails 3.1 应用程序,它运行良好!
但是现在我想在我的应用程序中完全集成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
你可以这样做:
User.create!(:email => data.email, :password => Devise.friendly_token[0,20], :authentication_token => access_token.credentials.token)
您还需要将 :authentication_token 或任何您命名的内容添加到attr_accessible
我正在寻找同样的东西。我试图实现詹姆斯·罗比的答案,并感到同样的错误:OAuthException :: An active access token must be used to query information about the current user
。然后我意识到authentication_token不是这样得救的。看了一会儿后,我在FbGraph + OmniAuth + Facebook Graph API on Rails应用程序中发现了一种使用会话变量的方法,通过在omniauth回调期间实现令牌保存,然后在调用fb_graph时使用它。所以它可能是这样的:
全身份验证回调(应用/控制器/用户/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
我不知道这是否是最好的方法,但到目前为止有效。
请您参考 https://github.com/nov/fb_graph/wiki/Page-Management
其中 nov 指出:您需要页面的访问令牌来管理您的页面。您(用户的)访问令牌在此处不起作用。
点击他链接到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 = 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)