knowndecimal/fulfil

View on GitHub
bin/authenticate

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true
 
require 'dotenv/load'
require 'bundler/setup'
 
require 'fulfil'
require 'oauth2'
 
def get_token
client_id = ENV.fetch('FULFIL_CLIENT_ID')
client_secret = ENV.fetch('FULFIL_CLIENT_SECRET')
subdomain = ENV.fetch('FULFIL_SUBDOMAIN')
 
base_url = "https://#{subdomain}.fulfil.io" # "oauth/authorize"
 
client = OAuth2::Client.new(client_id, client_secret, site: base_url)
authorize_url = client.auth_code.authorize_url(
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
scope: ['sale.channel:read'].join(','),
access_type: 'offline_access'
)
 
output 'Open this URL:'
system %x[echo "#{authorize_url.to_s}" | pbcopy]
output authorize_url.to_s
 
output 'Copy the generated token. Paste it here:'
 
auth_token = gets.chomp
output ''
token = client.auth_code.get_token(auth_token)
 
output 'Results:'
pp token.to_hash
output ''
 
output 'Offline access token:'
output token.to_hash.dig('offline_access_token')
end
 
def output(text, lines: 2)
puts text
puts "\n" * lines
end
 
def fulfil
@fulfil ||= Fulfil::Client.new(
subdomain: ENV.fetch('FULFIL_SUBDOMAIN'), token: ENV.fetch('FULFIL_OAUTH_TOKEN')
)
end
 
get_token