Rajesh Choudhary Rajesh Choudhary - 6 months ago 26
Ruby Question

undefined method `get_access_token'

My twitter controller code is this

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


On index page add your twitter account option is there. After clicking on that it will authorize app & After authorizaion process i am facing an error "undefined method `get_access_token' .i am facing error in this line " @access_token = @request_token.get_access_token(:oauth_verifier => params["oauth_verifier"])"

Answer

The solution of my problem: What i did is I added a new method to prepare access token & in get client method used Twitter::REST::Client.new do |config| and also provided access_token & access_token_secret_key and now it is working properly.

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