在rails app中实现koala gem时未定义的方法`get_access_token'

时间:2015-09-08 11:54:31

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems

我想在rails app中实现koala gem 但我得到一个错误“未定义的方法get_access_token” 我的facebook控制器代码是

  class FacebookController < ApplicationController
  before_filter :authenticate_user!

  def index
  unless current_user.facebook_oauth_setting
      @oauth = Koala::Facebook::OAuth.new("app-id", "secret", "http://#{request.host}:#{request.port}/callback")
      session["oauth_obj"] = @oauth
      redirect_to @oauth.url_for_oauth_code
    else
      redirect_to "/facebook_profile"
    end
  end

  def callback
    unless current_user.facebook_oauth_setting

      @oauth = session["oauth_obj"]
      Rails.logger.info("**************#{@oauth}***************")
      Rails.logger.info("**********#{params[:code]}*************")

   FacebookOauthSetting.create({:access_token => @oauth.get_access_token(params[:code]), :user_id => current_user.id})  

      redirect_to "/facebook_profile"
    else
      redirect_to "/"
    end
  end

  def facebook_profile
    if current_user.facebook_oauth_setting
      @graph = Koala::Facebook::API.new(current_user.facebook_oauth_setting.access_token)
      @profile = @graph.get_object("me")
      @picture = @graph.get_picture("me")
      @feed = @graph.get_connections("me","feed")
      @friends = @graph.get_connections("me", "friends")
    else
      redirect_to "/"
    end
  end

end

我存储访问令牌的模型是

class TwitterOauthSetting < ActiveRecord::Base
  belongs_to :user

end

我迁移到商店access_token是

class CreateFacebookOauthSettings < ActiveRecord::Migration
  def change
    create_table :facebook_oauth_settings do |t|
      t.string :access_token
      t.integer :user_id
      t.timestamps null: false
    end
  end
end

2 个答案:

答案 0 :(得分:2)

@oauth方法中创建callback对象,而不是从session引入它。所以这样做:

@oauth = Koala::Facebook::OAuth.new("app-id", "secret", "http://#{request.host}:#{request.port}/callback")    
FacebookOauthSetting.create(:access_token => @oauth.get_access_token(params[:code]), :user_id => current_user.id)

它应该可以解决你的问题。

答案 1 :(得分:-1)

对象类型应为Koala::Facebook