2

I'm a noob studying Ruby on Rails. I am trying to integrate my ruby on rails app to Quickbooks. I'm following the steps of this video. And I am stuck when I try to authenticate by pressing the "Connect to QuickBooks" button.

enter image description here

I get this error;

enter image description here

I did a little research and found out that this error was about the URI.escape() command. I think they removed it in Ruby 3. How can I resolve this issue? I can't even figure out where this URI.escape() command is located.

This is from vendors_controller.rb

def authenticate
    callback = oauth_callback_vendors_url
    token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback)
    session[:qb_request_token] = token
    # If Rails >= 4.1 you need to do this => session[:qb_request_token] = Marshal.dump(token)   
    redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
  end

  def oauth_callback
    at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
    # If Rails >= 4.1 you need to do this =>  at = Marshal.load(session[:qb_request_token]).get_access_token(:oauth_verifier => params[:oauth_verifier])
    session[:token] = at.token
    session[:secret] = at.secret
    session[:realm_id] = params['realmId']
    redirect_to root_url, notice: "Your QuickBooks account has been successfully linked."
  end

My initializer quickbooks.rb

QB_KEY = "I PASTED MY CLIENT ID KEY HERE"
QB_SECRET = "I PASTED MY CLIENT SECRET HERE"

$qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, {
    :site                 => "https://oauth.intuit.com",
    :request_token_path   => "/oauth/v1/get_request_token",
    :authorize_url        => "https://appcenter.intuit.com/Connect/Begin",
    :access_token_path    => "/oauth/v1/get_access_token"
})

My rails version Rails 6.1.4 My ruby version ruby 3.0.1p64

Thank you for your time.

Kvothe28
  • 74
  • 1
  • 12
  • 2
    That error will be coming from the gem you are using for OAuth. Doing a quick scan of the gems in the tutorial, the `oauth-plugin` gem it uses was last released in 2013, so is many ruby versions behind. You might look for a more recent tutorial. The `quickbooks-ruby` gem (https://github.com/ruckus/quickbooks-ruby) gem seems reasonably maintained and has some docs on its GitHub site you might check out. – rmlockerd Jun 29 '21 at 07:47
  • 1
    Another hint for the future: If you click on "Full trace" on the error page (you made a screenshot of), you can follow the backtrace and it should also give you a hint where the error comes from. Adding the full stacktrace to a question on SO also helps people to understand quicker how to help you. – trueunlessfalse Jun 29 '21 at 07:50
  • 1
    @rmlockerd Thank you, I am currently using `quickbooks-ruby` as well in my code. I asked people here to suggest a more reliable up-to-date guide or method but my question got downvoted and then deleted. I am looking for a better tutorial all over the web but none of them fits my conditions except this 7 years old video guides. – Kvothe28 Jun 29 '21 at 07:54
  • @trueunlessfalse I did and got this line; `oauth (0.4.7) lib/oauth/helper.rb:12:in 'escape' ` Now I know it is because of the helper located in `'oauth-plugin'` gem. – Kvothe28 Jun 29 '21 at 07:58
  • `URI.escape` is deprecated as you can see here: https://stackoverflow.com/questions/2824126/whats-the-difference-between-uri-escape-and-cgi-escape It looks like there's already a Github issue about this https://github.com/ruckus/quickbooks-ruby/issues/548 .. – max pleaner Jun 29 '21 at 08:02
  • @maxpleaner As I suspected. But how can I resolve this issue? As I understand this `URI.escape()` command is located in a helper file on `'oauth-plugin'` gem. How can I fix this do I just change the gem codes and replace `URI.escape()` with `CGI.escape()` ? – Kvothe28 Jun 29 '21 at 08:06
  • I also noticed that the quickbooks-ruby gem writes in their README that it is only tested on ruby 2.x. However as the error comes from the oauth-gem you might try to use a newer version of it. I just checked the Changelog for oauth2, and it doesn't mention official support for ruby 3. – trueunlessfalse Jun 29 '21 at 08:06
  • Yeah I was gonna suggest patching URI.escape to CGI.escape – max pleaner Jun 29 '21 at 08:11
  • 1
    One option for you could also be to start out with ruby 2.7 and upgrade to ruby 3 once the issue is fixed. Using rvm or rbenv might be helpful. – trueunlessfalse Jun 29 '21 at 08:11
  • @trueunlessfalse I think I can try 2.7.2 how can I rearrange my ruby version? Trying this for the firsst time. – Kvothe28 Jun 29 '21 at 08:39
  • @Kvothe28 sorry for the late reply. You can use https://github.com/rbenv/rbenv to switch your ruby version per project. – trueunlessfalse Jun 29 '21 at 10:42

3 Answers3

10

I know many people say that URI.escape wasn't good, but it was good enough for me when all I wanted from it is to escape urls with non-English characters.

So I just monkey patched it

Add the following under /config/initializers/functions_overrides.rb (the file name can be anything you want)

require 'uri'

module URI
    class << self
        def escape(str)
            alpha = "a-zA-Z"
            alnum = "#{alpha}\\d"
            unreserved = "\\-_.!~*'()#{alnum}"
            reserved = ";/?:@&=+$,\\[\\]"
            unsafe = Regexp.new("[^#{unreserved}#{reserved}]")
            str.gsub(unsafe) do
                us = $&
                tmp = ''
                us.each_byte do |uc|
                    tmp << sprintf('%%%02X', uc)
                end
                tmp
            end.force_encoding(Encoding::US_ASCII)
        end
    end
end
Oz Ben-David
  • 1,589
  • 1
  • 16
  • 26
2

Use the https://developer.intuit.com/app/developer/qbo/docs/develop/sdks-and-samples-collections/ruby

OAuth Ruby Client

Intuit offers an OAuth 2.0 Client which provides a set of methods that make it easier to work with Intuit’s OAuth and OpenID implementation.

Community Supported Ruby SDK

The Community Supported Ruby SDK makes it easy to integrate your web app with the QuickBooks Online API. This guide assumes that you have an existing web app that you want to integrate with QuickBooks Online.

Stephen Stilwell
  • 518
  • 1
  • 5
  • 17
0

URI#escape was deprecated and later removed - https://github.com/ruby/uri/commit/61c6a47ebf1f2726b60a2bbd70964d64e14b1f98

From the commit :

# This method is obsolete and should not be used. Instead, use
# CGI.escape, URI.encode_www_form or URI.encode_www_form_component
# depending on your specific use case.

Ruby 3.0.0 onwards URI#escape does not work.

Use CGI.escape or URI.encode_www_form as mentioned in the deprecation warning

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88