Last active
December 12, 2015 06:38
-
-
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.
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
# 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 |
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
source :rubygems | |
gem 'sinatra' | |
gem 'omniauth-createsend' | |
gem 'createsend' |
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
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