oauth-plugin icon indicating copy to clipboard operation
oauth-plugin copied to clipboard

How to override 'site' option for Freshbooks

Open kpgarrod opened this issue 11 years ago • 0 comments

I am authorising as a Freshbooks consumer with OAuth. Each of my users may have his own Freshbooks account. Each Freshbooks user has his own subdomain (e.g. example.freshbooks.com). In order to authorise, I have to use urls on that subdomain.

I couldn't find an easy way to change the 'site' option for each user. I ended up overriding OauthConsumersController#show with this:

  def show
    case params[:id]
    when 'freshbooks'
      request_url = callback_oauth_consumer_url(params[:id]) + callback2_querystring
      @request_token = @consumer.get_request_token(request_url)  
      @request_token.consumer.options['site'] ="https://#{params['subdomain']}.freshbooks.com"
      session[@request_token.token]=@request_token.secret 
      if @request_token.callback_confirmed?
        redirect_to @request_token.authorize_url
      else
        redirect_to(@request_token.authorize_url + "&oauth_callback=#{callback_oauth_consumer_url(params[:id])}")
      end
    else
      super
    end
  end 

It's far from beautiful and obviously fragile, so I would love to know the 'right' way of doing it.

BTW, it was not straightforward to configure Freshbooks, so I include my Token here in case it saves somebody some time:

require 'oauth/signature/plaintext'
class FreshbooksToken < ConsumerToken
  FRESHBOOKS_SETTINGS={
    :site => "",
    :request_token_path => "/oauth/oauth_request.php",
    :access_token_path => "/oauth/oauth_access.php",
    :authorize_path => "/oauth/oauth_authorize.php",
    :scheme=> :query_string, #this is the only one Freshbooks accepts
    :signature_method=>"PLAINTEXT",  # this is the only one Freshbooks accepts
    }


  def self.consumer(options={})
    @consumer ||= OAuth::Consumer.new(credentials[:key], credentials[:secret], FRESHBOOKS_SETTINGS.merge(options))
  end
end

Thanks in advance

kpgarrod avatar Jul 27 '13 12:07 kpgarrod