我在我的Rails3应用程序中使用了devise和omniauth-facebook进行Facebook身份验证,基于这个教程:Adding Facebook auth to Rails 3.1 app,它工作得很好!
但现在我想要在我的应用程序中完全集成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发布于 2012-04-11 18:01:07
您可以这样做:
User.create!(:email => data.email, :password => Devise.friendly_token[0,20], :authentication_token => access_token.credentials.token)您还需要将:authentication_token或您为其命名的任何名称添加到attr_accessible
发布于 2012-06-22 08:37:19
我也在找同样的东西。我尝试实现James Robey{的答案,但遇到了同样的错误: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回调(app/controllers/users/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我不知道这是不是最好的方法,但到目前为止还行得通。
发布于 2013-05-09 16:54:53
我推荐您访问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有一些内部工作机制,但关键是,当您从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)https://stackoverflow.com/questions/10060738
复制相似问题