Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active December 12, 2015 06:38
Show Gist options
  • Save jdennes/4730102 to your computer and use it in GitHub Desktop.
Save jdennes/4730102 to your computer and use it in GitHub Desktop.
Rack application to demonstrate authenticating with the Campaign Monitor API using OAuth. Uses the sinatra, omniauth-createsend, and createsend gems.
# 1. Set CREATESEND_CLIENT_ID and CREATESEND_CLIENT_SECRET environment
# variables for your registered OAuth application.
# 2. `bundle install`
# 3. `rackup`
require 'bundler/setup'
require 'sinatra/base'
require 'omniauth-createsend'
require 'createsend'
class App < Sinatra::Base
get '/' do
redirect '/auth/createsend'
end
get '/auth/createsend/callback' do
access_token = request.env['omniauth.auth']['credentials']['token']
refresh_token = request.env['omniauth.auth']['credentials']['refresh_token']
expires_at = request.env['omniauth.auth']['credentials']['expires_at']
response = "Your user is successfully authenticated. Here are the details you need:<br/><br/>"
response << "access token: #{access_token}<br/>"
response << "refresh token: #{refresh_token}<br/>"
response << "expires at: #{expires_at}<br/>"
response << "<br/><br/>"
auth = {
:access_token => access_token,
:refresh_token => refresh_token }
cs = CreateSend::CreateSend.new auth
clients = cs.clients
response << "We've made an API call too. Here are your clients:<br/><br/>"
response << MultiJson.encode(clients)
response
end
get '/auth/failure' do
content_type 'application/json'
MultiJson.encode(request.env)
end
end
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :createsend, ENV['CREATESEND_CLIENT_ID'], ENV['CREATESEND_CLIENT_SECRET'],
:scope => 'ViewReports,CreateCampaigns,SendCampaigns'
end
run App.new
source :rubygems
gem 'sinatra'
gem 'omniauth-createsend'
gem 'createsend'
web: bundle exec rackup config.ru -p $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment