Sha256: 0f8b919caa7103153959bb8e38100ab01842a5d7087067cf2a8c39527ca5f591

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# Setup
#
# gem install bundler
# Ensure you have http://localhost:5678 (or PORT) as a Redirect URI in QBO.

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'dotenv'
  gem 'ledger_sync'
  gem 'rack'
  gem 'pd_ruby', '0.2.2', require: true
end

puts 'Gems installed and loaded!'

require 'socket'
require 'dotenv'
require 'rack'
require 'ledger_sync'
require 'rack/lobster'

Dotenv.load

port = ENV.fetch('PORT', 5678)
app = Rack::Lobster.new
server = TCPServer.new port

base_url = "http://localhost:#{port}"

puts "Listening at #{base_url}"

client_id = ENV.fetch('QUICKBOOKS_ONLINE_CLIENT_ID')

raise 'QUICKBOOKS_ONLINE_CLIENT_ID not set in ../.env' if client_id.blank?

adaptor = LedgerSync::Adaptors::QuickBooksOnline::Adaptor.new_from_env(test: true)

puts 'Go to the following URL:'
puts adaptor.authorization_url(redirect_uri: base_url)

while session = server.accept
  request = session.gets

  puts request

  # 1
  method, full_path = request.split(' ')

  # 2
  path, query = full_path.split('?')

  params = Hash[query.split('&').map { |e| e.split('=') }] if query.present?

  adaptor.set_credentials_from_oauth_code(
    code: params.fetch('code'),
    realm_id: params.fetch('realmId'),
    redirect_uri: base_url
  )

  puts "\n"

  puts 'access_token:'
  puts adaptor.access_token
  puts "\n"
  puts 'client_id:'
  puts adaptor.client_id
  puts "\n"
  puts 'client_secret:'
  puts adaptor.client_secret
  puts "\n"
  puts 'realm_id:'
  puts adaptor.realm_id
  puts "\n"
  puts 'refresh_token:'
  puts adaptor.refresh_token
  puts "\n"

  puts 'Done!'

  status = 200
  body = 'Done'
  headers = {
    'Content-Length' => body.size
  }

  session.print "HTTP/1.1 #{status}\r\n"

  headers.each do |key, value|
    session.print "#{key}: #{value}\r\n"
  end

  session.print "\r\n"

  session.print body

  session.close

  break
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 bin/quickbooks_online_oauth_server.rb