arsduo/koala · error · ArgumentError

build_url must get a callback either from the OAuth object o

Error message

build_url must get a callback either from the OAuth object or in the parameters!

What it means

url_for_oauth_code, url_for_access_token, and url_for_dialog all delegate to build_url with require_redirect_uri = true. The redirect URI must come from url_options[:redirect_uri] (the legacy alias :callback is also accepted) or from @oauth_callback_url, the third argument of Koala::Facebook::OAuth.new; if none is present, build_url raises ArgumentError because a Facebook dialog or token URL without a callback cannot complete the flow.

Source

Thrown at lib/koala/oauth.rb:335

      # base 64
      # directly from https://github.com/facebook/crypto-request-examples/raw/master/sample.rb
      def base64_url_decode(str)
        str += '=' * (4 - str.length.modulo(4))
        Base64.decode64(str.tr('-_', '+/'))
      end

      def server_url(type)
        url = "https://#{Koala.config.send(type)}"
        if version = Koala.config.api_version
          "#{url}/#{version}"
        else
          url
        end
      end

      def build_url(type, path, require_redirect_uri = false, url_options = {})
        if require_redirect_uri && !(url_options[:redirect_uri] ||= url_options.delete(:callback) || @oauth_callback_url)
          raise ArgumentError, "build_url must get a callback either from the OAuth object or in the parameters!"
        end
        params = Koala::HTTPService.encode_params(url_options)
        "#{server_url(type)}#{path}?#{params}"
      end
    end
  end
end

View on GitHub (pinned to 47d052063e)

Solutions

  1. Pass the callback as the third constructor argument: Koala::Facebook::OAuth.new(app_id, app_secret, "https://example.com/auth/facebook/callback").
  2. Or set it once globally with Koala.configure { |config| config.oauth_callback_url = ENV["FB_CALLBACK_URL"] }.
  3. Or pass :redirect_uri per call for flows that intentionally use a different callback.
  4. Use the exact option keys :redirect_uri or :callback; anything else is ignored and reproduces the error.

Example fix

// before
@oauth = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET)
url = @oauth.url_for_oauth_code

// after
@oauth = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET, "https://example.com/auth/facebook/callback")
url = @oauth.url_for_oauth_code
Defensive patterns

Strategy: validation

Validate before calling

cb = ENV["FB_CALLBACK_URL"]
raise ArgumentError, "FB_CALLBACK_URL not configured" if cb.to_s.empty?
@oauth = Koala::Facebook::OAuth.new(ENV.fetch("FB_APP_ID"), ENV.fetch("FB_APP_SECRET"), cb)

Prevention

When it happens

Trigger: Building OAuth.new(app_id, app_secret) without the third argument and then calling url_for_oauth_code, url_for_access_token(code), or url_for_dialog(:feed) without :redirect_uri in the options; or Koala.config.oauth_callback_url being nil in an environment where the constructor argument is also omitted.

Common situations: Multi-environment apps where only production sets the callback; refactors that drop the third constructor argument; passing the unsupported key :callback_url (accepted keys are :redirect_uri and :callback) and silently still getting nil.

Related errors


AI-assisted analysis of arsduo/koala@47d052063e (2026-08-23). Data as JSON: /api/errors/03b9e8397c591b04. Report an issue: GitHub.