Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Created October 27, 2010 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbmeeks/649915 to your computer and use it in GitHub Desktop.
Save cbmeeks/649915 to your computer and use it in GitHub Desktop.
TripIt OAuth Example for http://flocktravel.com
class ThirdpartyController < ApplicationController
before_filter :authenticate_user!
before_filter :get_consumer_info, :except => [ :index ]
def index
session[:request_token] = nil
@tripit = current_user.authorizations.where(:provider => "tripit")
end
def tripit
session[:request_token] = nil
consumer = OAuth::Consumer.new @api_key, @api_secret, { :site => "https://api.tripit.com" }
callback = CGI::escape(APP_CONFIG['tripit_callback_url'])
request_token = consumer.get_request_token
oauth_token = request_token.token
request_url = "https://www.tripit.com/oauth/authorize?oauth_token=#{oauth_token}&oauth_callback=#{callback}"
request_token.consumer.secret = nil # do not pass the consumer secret via session
session[:request_token] = request_token
redirect_to request_url
end
def tripit_remove
authorizations = current_user.authorizations.where(:provider => "tripit") # incase we got more than one tripit
authorizations.each do |a|
a.destroy
end
flash[:notice] = "Successfully removed TripIt! from your account."
redirect_to thirdparty_path
end
def tripit_callback
request_token = session[:request_token]
request_token.consumer.secret = @api_secret
session[:request_token] = nil
access_token = request_token.get_access_token
token = access_token.token
secret = access_token.secret
authorization = Authorization.find_by_user_id_and_provider(current_user.id, "tripit")
if authorization
authorization.token = token
authorization.secret = secret
authorization.save!
else
current_user.authorizations.create!(:provider => "tripit", :token => token, :secret => secret )
end
flash[:notice] = "Successfully authenticated TripIt!"
redirect_to thirdparty_path
end
private
def get_consumer_info
@api_key = APP_CONFIG['tripit_api_key']
@api_secret = APP_CONFIG['tripit_api_secret']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment