jrallison / authlogic_oauth

Authlogic OAuth is an extension of the Authlogic library to add OAuth support. OAuth can be used to allow users to login with their Twitter credentials.
authlogic-oauth.heroku.com
MIT License
165 stars 25 forks source link

Getting twitter username after successful auth / registration #12

Closed natikgadzhi closed 7 months ago

natikgadzhi commented 14 years ago

Hi everyone.

Is it possible to get twitter username / user id from authlogic_oauth after twitter authentication?

I see twitter user id in the live example, but i still didn't get it to work in my application, i get NO additional information at all and no information on twtiiter_uid user column at all.

How you guys get additional user information from oauth?

hexorx commented 14 years ago

In order to interact with twitter you need to use the oauth access token. But they make that a private method so to get at it you need to add an extra method to your user model.

class User < ActiveRecord::Base
  acts_as_authentic

  def twitter_profile
    @twitter_profile ||= JSON.parse(access_token.get('/account/verify_credentials.json').body)
  end
end

That will give you a hash with all sorts of info about the user. Now since it is just parsing the JSON twitter returns you need to use strings for the key. But to get the twitter username you would do something like this.

@user.twitter_profile['screen_name']

Hope that helps.

Follow Me: @JoshRobinson