mercredi 2 septembre 2015

Configure Twitter Api for multiple users

I want to configure my twitter app for multiple users.My twitter controller code is

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"})
    Rails.logger.info("***********#{@consumer}*****************")
    @request_token = @consumer.get_request_token(:oauth_callback => oauth_callback)
    session[:request_token] = @request_token.token
    session[:request_secret] = @request_token.secret

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


  def oauth_account
    if TwitterOauthSetting.find_by_user_id(current_user.id).nil?
       @consumer = OAuth::Consumer.new("2K763Dgw9y6oAOOLsegegkHW7","pwXauJeR628SL8DhgwikStNYykGCKoabISHI4ZUnKIxt2eSmNY", {: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"])
      session[:oauth_token] = @access_token.token
      session[:oauth_secret] = @access_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 = @client.user_timeline
    @home_timeline = @client.home_timeline
  end

  private
  def update_user_account
    user_twitter_profile = @client.user
    Rails.logger.info("**************#{user_twitter_profile}****************")
    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

and in config/intializer/twitter.rb

@client = Twitter::REST::Client.new do |config|
    config.consumer_key = 'consumer_key '
    config.consumer_secret = ' consumer_secret' 
    config.access_token= ' access_token '
    config.access_token_secret= 'access_token_secret '
end

For this i am using gem "http://ift.tt/1Urdtub". But I am getting timeline of account from which i created twitter API even i logged in from different account.I want that for every user who signed in through twitter API will be able to see their own timeline

Aucun commentaire:

Enregistrer un commentaire