在 rails 应用程序中实现考拉 gem 时未定义的方法“get_access_token”

undefined method `get_access_token' while implementing koala gem in rails app

我想在 rails 应用中实现 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

callback 方法中创建 @oauth 对象,而不是从 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)

应该可以解决您的问题。

对象类型应为 Koala::Facebook