简体   繁体   中英

How can I get oauth token and oauth token secret using the ruby omniauth-twitter gem?

I setup my app similar to the tutorial here - http://railscasts.com/episodes/235-devise-and-omniauth-revised . If you cant access it, below is my code

Omniauth controller callback

  def all
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :twitter, :all
end

user model

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
      user.name = auth.info.name

    end
  end

  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

Now I am wondering how do I obtain the authenticated user's oauth token and oauth token secret?

Thanks

If you want to take a look at the information returned by a certain provider, put this as the first line of your callback controller:

raise env["omniauth.auth"].to_yaml

You'll be able to see that the information you want can be accessed in auth.credentials.token and auth.credentials.secret .

EDIT: Now that Rails 4 uses the better_errors gem, this method of inspecting the omniauth hash no longer works so well. A better way now is:

render :text => "<pre>" + env["omniauth.auth"].to_yaml and return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM