Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active October 14, 2021 05:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdennes/4945412 to your computer and use it in GitHub Desktop.
Save jdennes/4945412 to your computer and use it in GitHub Desktop.
Another example Rack application to demonstrate authenticating with the Campaign Monitor API using OAuth. This example only uses the sinatra and createsend gems.
# 1. Set CREATESEND_CLIENT_ID, CREATESEND_CLIENT_SECRET,
# and CREATESEND_REDIRECT_URI environment variables for
# your registered OAuth application.
# 2. `bundle install`
# 3. `rackup`
require 'bundler/setup'
require 'sinatra/base'
require 'createsend'
class App < Sinatra::Base
get '/' do
redirect CreateSend::CreateSend.authorize_url(
ENV['CREATESEND_CLIENT_ID'], ENV['CREATESEND_REDIRECT_URI'],
'ViewReports,CreateCampaigns,SendCampaigns')
end
get '/exchange_token' do
code = params['code']
access_token, expires_in, refresh_token =
CreateSend::CreateSend.exchange_token ENV['CREATESEND_CLIENT_ID'],
ENV['CREATESEND_CLIENT_SECRET'], ENV['CREATESEND_REDIRECT_URI'], code
response = "<pre>"
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 in: #{expires_in}<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 << "</pre>"
response
end
end
run App.new
source :rubygems
gem 'sinatra'
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