Search code examples
rubytwittersinatraomniauthkoala

Koala and Omniauth-twitter don't go together


I posted the same in github of Koala but noone answered to me so I put here.

So when I try to login with Twitter with Omniauth:

I, [2013-11-15T18:57:12.371006 #28412]  INFO -- omniauth: (twitter) Request phase      initiated.
127.0.0.1 - - [15/Nov/2013 18:57:13] "GET /auth/twitter HTTP/1.1" 500 144366 0.9355

I have also a Koala login to facebook I don't use Omniauth for Facebook I just use Omniauth for twitter, If I don't require Koala is ok, but if I have both it generates:

undefined method `[]=' for #<Koala::Facebook::OAuth:0x00000001b03348>
~>In oauth.rb line 31

I'm using 1.6.0 version of Koala and Sinatra.

My code is:

#Facebook
get '/loginfb' do
    session['oauth'] = Koala::Facebook::OAuth.new($APP_ID, $APP_SECRET, "#{request.base_url}/callbackfb")
    redirect session['oauth'].url_for_oauth_code(:permissions => ["publish_stream"])
end

get '/callbackfb' do
    session['access_token'] = session['oauth'].get_access_token(params[:code])
    registerUserFB() #Just register the user function
    redirect '/accounts'
end

#Twitter
#By defualt logs in with /auth/twitter
get '/auth/twitter/callback' do
    erb "<h1>#{params[:provider]}</h1><pre>#{JSON.pretty_generate(request.env['omniauth.auth'])}</pre>"
    p auth['credentials']['token']
end

get '/auth/failure' do
    erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>"
end

Thanks guys in advance.


Solution

  • I used another gem for logging with twitter called twitter_oauth yo can find here

    For use with sinatra is quite simple:

    #Sinatra stuff
    require 'twitter_oauth'
    #more sinatra stuff
    
    $CONSUMER_KEY = '32423...'
    $CONSUMER_SECRET = '...adads...'
    $CALLBACK_URL = 'http://....'
    
    tw_client = TwitterOAuth::Client.new(
        :consumer_key => $CONSUMER_KEY,
        :consumer_secret => $CONSUMER_SECRET
    )
    
    $request_token = tw_client.request_token(:oauth_callback => $CALLBACK_URL)
    
    #sinatra routes
    get '/logintw' do
        redirect $request_token.authorize_url
    end
    
    get '/callbacktw' do
        @access_token = $request_token.get_access_token :oauth_verifier =>     params[:oauth_verifier]
        p @access_token.params[:oauth_token]
        p @access_token.params[:oauth_token_secret]
        p @access_token.params[:screen_name]
        p @access_token.params[:user_id]
        redirect '/accounts'
    end
    #more sinatra routes
    

    Is not the best solution but is one and for me works!

    Thanks anyway.