tbrooke
2/26/2014 - 12:38 AM

gistfile1.rb

require 'clio_client'
require 'date'

def token_file; "ClioToken.txt"; end

saved_token = nil

client_id = "YOUR-APP-KEY-HERE"
client_secret = "YOUR-APP-SECRET-HERE"
client = ClioClient::Session.new({client_id: client_id, client_secret: client_secret})

if File.exists?(token_file)
	file = File.open(token_file, "r")
	saved_token = file.gets
	file.close
	end
	
if saved_token
	client.access_token = saved_token
else
	puts "Paste following URL in a browser and accept the connection"
	puts "https://app.goclio.com/oauth/authorize?response_type=code&client_id=#{client_id}&redirect_uri=https%3A%2F%2Fapp.goclio.com%2Foauth%2Fapproval\n"

	client.authorize_url("https://app.goclio.com/oauth/approval")

	print "Enter code returned from Clio: "
	code = gets.chomp
	client.authorize_with_code "https://app.goclio.com/oauth/approval", code.to_str
	
	if client.authorized?
		saved_token = client.access_token
		end

	end

if !client.authorized?
	abort "Unauthorized"
	end
		
if saved_token and !File.exists?(token_file)
	file = File.new(token_file, "w")
	file.write(saved_token)
	file.close
	end

me = client.users.who_am_i
puts "Token saved for #{me[1].first_name} #{me[1].last_name} of #{me[0].name}"