-
-
Save dragossh/9f5fa66581e24dc9c85f to your computer and use it in GitHub Desktop.
Sample Sinatra app for fetching the FreeAgent API Access and Refresh tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
require 'net/http' | |
require 'json' | |
class App < Sinatra::Base | |
FA_URI = URI('https://api.freeagent.com') | |
CLIENT_ID = 'abc' # put client id from dev dashboard here | |
CLIENT_SECRET = 'abc' # put client secret from dev dashboard here | |
USE_SSL = true | |
def initialize | |
super | |
@@auth_token = nil | |
@@access_token = nil | |
@@refresh_token = nil | |
end | |
# Show current state | |
get '/' do | |
if @@auth_token.nil? && @@access_token.nil? && @refresh_code.nil? | |
# Link to the authorization request, also needs a redirect_uri if that is not set in the dev dashboard | |
"<a href=\"#{FA_URI.to_s}/v2/approve_app?response_type=code&client_id=#{CLIENT_ID}\">Authorize</a>" | |
elsif @@auth_token && !@@access_token && !@@refresh_token | |
"Auth token: #{@@auth_token}<br />" << | |
'<a href="/exchange_token">Exchange token for access and refresh tokens</a>' | |
else | |
"Access token: #{@@access_token}<br />" << | |
"Refresh token: #{@@refresh_token}<br />" << | |
'<a href="/reset">Reset codes</a>' | |
end | |
end | |
# Step 1: Fetch Authorization token afte the user clicked "Authorize" | |
get '/auth_endpoint' do | |
code = params[:code] | |
error = params[:error] | |
return params[:error_description] if error | |
if code | |
@@auth_token = code | |
redirect to('/') | |
end | |
end | |
# Step 2: Exchange tokens once we have the authorization code | |
get '/exchange_token' do | |
if @@auth_token | |
http = Net::HTTP.new(FA_URI.hostname, FA_URI.port) | |
http.use_ssl = USE_SSL | |
req = Net::HTTP::Post.new('/v2/token_endpoint') | |
req['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' | |
req.body = "grant_type=authorization_code&code=" << @@auth_token # also needs a redirect_uri if that is not set in the dev dashboard | |
req.basic_auth CLIENT_ID, CLIENT_SECRET | |
res = http.request(req) | |
if res.kind_of?(Net::HTTPOK) | |
json = JSON.parse(res.body) | |
@@access_token = json['access_token'] | |
@@refresh_token = json['refresh_token'] | |
redirect to('/') | |
else | |
'Failed to get access and refresh tokens: ' << res.body | |
end | |
else | |
redirect to('/') | |
end | |
end | |
get '/reset' do | |
@@auth_token = nil | |
@@access_token = nil | |
@@refresh_token = nil | |
redirect to ('/') | |
end | |
end | |
App.run! :port => 4567 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment