Last active
October 14, 2021 05:17
-
-
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.
This file contains hidden or 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, 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 |
This file contains hidden or 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 'createsend' |
This file contains hidden or 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