如何让考拉与Omniauth一起玩得很好?

Sim*_*ton 10 ruby-on-rails facebook-graph-api omniauth ruby-on-rails-3 koala

我正试图让考拉与Omniauth合作.用户模型使用Omniauth登录Facebook,我想使用Koala作为客户端来提取正在使用该应用程序的用户的朋友列表.我似乎没有正确保存令牌:

调节器

@friends = Array.new
 if current_user.token
   graph = Koala::Facebook::GraphAPI.new(current_user.token)
   @profile_image = graph.get_picture("me")
   @fbprofile = graph.get_object("me")
   @friends = graph.get_connections("me", "friends")
end
Run Code Online (Sandbox Code Playgroud)

DB Schema

create_table "users", :force => true do |t|
  t.string   "provider"
  t.string   "uid"
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "token"
end
Run Code Online (Sandbox Code Playgroud)

用户模型有

def self.create_with_omniauth(auth)  
  create! do |user|  
    user.provider = auth["provider"]  
    user.uid = auth["uid"]  
    user.name = auth["user_info"]["name"]  
  end  
end
Run Code Online (Sandbox Code Playgroud)

Koala.rb初始化程序有:

module Facebook
  CONFIG = YAML.load_file(Rails.root.join("config/facebook.yml"))[Rails.env]
  APP_ID = CONFIG['app_id']
  SECRET = CONFIG['secret_key']
end

Koala::Facebook::OAuth.class_eval do
  def initialize_with_default_settings(*args)
    case args.size
      when 0, 1
        raise "application id and/or secret are not specified in the config" unless Facebook::APP_ID && Facebook::SECRET
        initialize_without_default_settings(Facebook::APP_ID.to_s, Facebook::SECRET.to_s, args.first)
      when 2, 3
        initialize_without_default_settings(*args) 
    end
  end 

  alias_method_chain :initialize, :default_settings 
end
Run Code Online (Sandbox Code Playgroud)

会话控制器有:

  def create  
    auth = request.env["omniauth.auth"]  
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id  

    session['fb_auth'] = request.env['omniauth.auth']
    session['fb_access_token'] = omniauth['credentials']['token']
    session['fb_error'] = nil

    redirect_to root_url 
  end  
Run Code Online (Sandbox Code Playgroud)

Adi*_*ghi 12

您已经知道的问题是fb_access_token仅在当前会话中可用且不可用于Koala.

您的用户模型是否有一个存储"令牌"的列?如果没有,请确保您在用户模型中有该列.当您在用户模型中具有该列时,您将需要在创建用户时在其中存储内容(User类中的create_with_omniauth方法).在从facebook成功授权后,您会发现令牌字段填充了facebook oauth令牌.如果已填充,那么您的考拉代码应该可以使用.在这种情况下,不需要在会话中存储facebook凭证.

但是,如果您没有从Facebook进行离线访问(这意味着访问仅在短时间内提供,那么在会话中存储facebook凭据是有道理的.在这种情况下,您不应该使用"current_user.token"而是会话[" fb_auth_token"]而不是考拉.

希望这可以帮助!

因此,如果您想要离线访问(Facebook授权的长期存储),请更改您的型号代码以存储fb_auth_token,如下所示

# User model
def self.create_with_omniauth(auth)  
  create! do |user|  
    user.provider = auth["provider"]  
    user.uid = auth["uid"]  
    user.name = auth["user_info"]["name"]  
    user.token = auth['credentials']['token']
  end  
end

# SessionsController
def create  
  auth = request.env["omniauth.auth"]  
  user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
  # Note i've also passed the omniauth object      

  session[:user_id] = user.id  
  session['fb_auth'] = auth
  session['fb_access_token'] = auth['credentials']['token']
  session['fb_error'] = nil

  redirect_to root_url 
end 
Run Code Online (Sandbox Code Playgroud)

如果您有短期访问权限,请更改"其他"控制器以使用会话

# The other controller
def whateverthissactionis
  @friends = Array.new
  if session["fb_access_token"].present?
    graph = Koala::Facebook::GraphAPI.new(session["fb_access_token"]) # Note that i'm using session here
    @profile_image = graph.get_picture("me")
    @fbprofile = graph.get_object("me")
    @friends = graph.get_connections("me", "friends")
  end
end
Run Code Online (Sandbox Code Playgroud)