未定义的方法`get_access_token'

undefined method `get_access_token'

我的 Twitter 控制器代码是这样的

class TwitterController < ApplicationController
  before_filter :authenticate_user!

  def index
     unless TwitterOauthSetting.find_by_user_id(current_user.id).nil?
      redirect_to "/twitter_profile"
    end
  end

  def generate_twitter_oauth_url
    oauth_callback = "http://#{request.host}:#{request.port}/oauth_account"

    @consumer = OAuth::Consumer.new("n3yfOi9iEHwnyz5uEsyNotW6t","kqHFm5dRRX00dIQSwBcTEJKZNAzrWcuK0BOAyDVWY8liRI1cPc", :site => "https://api.twitter.com")

    @request_token = @consumer.get_request_token(:oauth_callback => oauth_callback)
    session[:request_token] = @request_token

    redirect_to @request_token.authorize_url(:oauth_callback => oauth_callback)
  end


  def oauth_account
    if TwitterOauthSetting.find_by_user_id(current_user.id).nil?
      @request_token = session[:request_token]
      @access_token = @request_token.get_access_token(:oauth_verifier => params["oauth_verifier"])
      TwitterOauthSetting.create(atoken: @access_token.token, asecret: @access_token.secret, user_id: current_user.id)
      update_user_account()
    end
    redirect_to "/twitter_profile"
  end

  def twitter_profile
    @user_timeline = get_client.user_timeline
    @home_timeline = get_client.home_timeline
  end

private

  def get_client    
    Twitter.configure do |config|
      config.consumer_key        = "n3yfOi9iEHwnyz5uEsyNotW6t"
      config.consumer_secret     = "kqHFm5dRRX00dIQSwBcTEJKZNAzrWcuK0BOAyDVWY8liRI1cPc"
    end

    Twitter::Client.new(
      :oauth_token => TwitterOauthSetting.find_by_user_id(current_user.id).atoken,
      :oauth_token_secret => TwitterOauthSetting.find_by_user_id(current_user.id).asecret
    )
  end

  def update_user_account
    user_twitter_profile = get_client.user
    current_user.update_attributes({
      name: user_twitter_profile.name, 
      screen_name: user_twitter_profile.screen_name, 
      url: user_twitter_profile.url, 
      profile_image_url: user_twitter_profile.profile_image_url, 
      location: user_twitter_profile.location, 
      description: user_twitter_profile.description
    })
  end

end

在索引页上添加您的 Twitter 帐户选项。单击后它将授权应用程序并在授权过程后我遇到错误 "undefined method `get_access_token' .i am facing error in this line " @access_token = @request_token.get_access_token(:oauth_verifier => params["oauth_verifier"])"

使用记录器检查 @acces_token 的值。也许它没有获得所需的值或数据类型。

我的问题的解决方法: 我所做的是我添加了一种新方法来准备访问令牌,并在获取客户端方法中使用 Twitter::REST::Client.new do |config| 并且还提供了 access_token & access_token_secret_key 现在它工作正常。

class TwitterController < ApplicationController
  before_filter :authenticate_user!

  def index
    unless TwitterOauthSetting.find_by_user_id(current_user.id).nil?
      redirect_to "/twitter_profile"
    end
  end

  def generate_twitter_oauth_url

    oauth_callback = "http://#{request.host}:#{request.port}/oauth_account"
    @consumer = OAuth::Consumer.new("2K763Dgw9y6oAOOLsegegkHW7","pwXauJeR628SL8DhgwikStNYykGCKoabISHI4ZUnKIxt2eSmNY", :site => "https://api.twitter.com")
    @request_token = @consumer.get_request_token(:oauth_callback => oauth_callback)

    session[:request_token] = @request_token
    redirect_to @request_token.authorize_url(:oauth_callback => oauth_callback)

  end


  def oauth_account
    if TwitterOauthSetting.find_by_user_id(current_user.id).nil?
      @request_token = session[:request_token]
      prepare_access_token(params[:oauth_token],params[:oauth_token_secret])
      @consumer = OAuth::Consumer.new(params[:oauth_token],params[:oauth_token_secret], :site => "https://api.twitter.com")
      @access_token = prepare_access_token(params[:oauth_token],params[:oauth_token_secret])
      TwitterOauthSetting.create(atoken: @access_token.token, asecret: @access_token.secret, user_id: current_user.id)
      update_user_account()
    end
    redirect_to "/twitter_profile"
  end

  def twitter_profile
    @user_timeline = get_client.user_timeline
    @home_timeline = get_client.home_timeline
  end

private

  def get_client    
   Twitter::REST::Client.new do |config|
      config.consumer_key = "n3yfOi9iEHwnyz5uEsyNotW6t"
      config.consumer_secret  = "kqHFm5dRRX00dIQSwBcTEJKZNAzrWcuK0BOAyDVWY8liRI1cPc"
      config.access_token = "access_token key"
      config.access_token_secret = "access_token_secret key"
    end
  end

  def update_user_account
    user_twitter_profile = get_client.user
    current_user.update_attributes({
      name: user_twitter_profile.name, 
      screen_name: user_twitter_profile.screen_name, 
      url: user_twitter_profile.url, 
      profile_image_url: user_twitter_profile.profile_image_url, 
      location: user_twitter_profile.location, 
      description: user_twitter_profile.description
    })
  end

  def prepare_access_token(oauth_token, oauth_token_secret)
      #consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :scheme => :header })
      @consumer = OAuth::Consumer.new("2K763Dgw9y6oAOOLsegegkHW7","pwXauJeR628SL8DhgwikStNYykGCKoabISHI4ZUnKIxt2eSmNY", { :site => "https://api.twitter.com", :scheme => :header })

      # now create the access token object from passed values
      token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
      access_token = OAuth::AccessToken.from_hash(@consumer, token_hash )

      return access_token
  end


end

您的 @request_token 显然是一个字符串,这就是 get_access_token 方法未定义的原因。

oauth_account 方法替换为下面的方法以重新创建 @request_token,因为 @request_token = session[:request_token] 将创建一个字符串,因此 get_access_token 方法未定义。

def oauth_account
    if TwitterOauthSetting.find_by_user_id(current_user.id).nil?

      # Re-create the request token
      @consumer = OAuth::Consumer.new(ENV["consumer_key"], ENV["consumer_secret"], :site => "https://api.twitter.com")
      @request_token = OAuth::RequestToken.new(@consumer, session[:request_token], session[:request_secret])

      @access_token = @request_token.get_access_token(:oauth_verifier => params["oauth_verifier"])
      TwitterOauthSetting.create(atoken: @access_token.token, asecret: @access_token.secret, user_id: current_user.id)
      update_user_account()
    end
        redirect_to "/twitter_profile"
    end

get_client 方法替换为以下方法以访问存储在会话中的用户令牌及其访问权限。

def get_client
    Twitter::REST::Client.new do |config|
      config.consumer_key = ENV["consumer_key"]
          config.consumer_secret = ENV["consumer_secret"]

      unless TwitterOauthSetting.find_by_user_id(current_user.id).nil?
        config.oauth_token = TwitterOauthSetting.find_by_user_id(current_user.id).atoken
        config.oauth_token_secret = TwitterOauthSetting.find_by_user_id(current_user.id).asecret
      end

    end
end