require 'oauth/consumer' require 'oauth/signature/plaintext' class FreshbooksOauth # initialize the oauth consumer, and also access token if user_token and user_secret provided def initialize( user_url ) @consumer = OAuth::Consumer.new("Your Freshbooks Consumer Key", "Your FreshBooks OAuth Secret", { :site => "https://"+user_url+".freshbooks.com", :request_token_path => "/oauth/oauth_request.php", :authorize_path => "/oauth/oauth_authorize.php", :access_token_path => "/oauth/oauth_access.php", :signature_method => 'PLAINTEXT', :oauth_version => '1.0a' }) end # returns the consumer def consumer @consumer end # returns the access token, also initializes new access token if user_token and user_secret provided def access_token( user_token = nil, user_secret = nil ) ( user_token && user_secret ) ? @access_token = OAuth::AccessToken.new( self.consumer, user_token, user_secret ) : @access_token end def access_token=(new_access_token) @access_token = new_access_token || false end # when the callback has been received, exchange the request token for an access token def exchange_request_for_access_token( request_token, request_token_secret, oauth_verifier ) #request_token = self.request_token( request_token, request_token_secret ) request_token = OAuth::RequestToken.new(self.consumer, request_token, request_token_secret) #Exchange the request token for an access token. this may get 401 error self.access_token = request_token.get_access_token( :oauth_verifier => oauth_verifier ) rescue => err puts "Exception in exchange_request_for_access_token: #{err}" raise err end # gets a request token to be used for the authorization request to freshbooks def get_request_token( oauth_callback ) self.consumer.get_request_token( :oauth_callback => oauth_callback ) end end