Last active
December 31, 2021 09:24
-
-
Save jdennes/4754097 to your computer and use it in GitHub Desktop.
Flask application to demonstrate authenticating with the Campaign Monitor API using OAuth. Uses the createsend, Flask, and Flask-OAuth packages.
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 and CREATESEND_CLIENT_SECRET environment | |
# variables for your registered OAuth application. Also set SESSION_SECRET | |
# environment variable for Flask. | |
# 2. `pip install -r requirements.txt` | |
# 3. `python app.py` | |
import os | |
from flask import Flask, render_template, request, redirect, url_for | |
from flask_oauth import OAuth | |
from createsend import * | |
import json | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = os.environ['SESSION_SECRET'] | |
oauth = OAuth() | |
campaign_monitor = oauth.remote_app('campaign monitor', | |
base_url='https://api.createsend.com', | |
request_token_url=None, | |
access_token_url='/oauth/token', | |
authorize_url='/oauth', | |
consumer_key=os.environ['CREATESEND_CLIENT_ID'], | |
consumer_secret=os.environ['CREATESEND_CLIENT_SECRET'], | |
request_token_params={'type': 'web_server', 'scope': 'ViewReports,CreateCampaigns,SendCampaigns'}, | |
access_token_params={'grant_type': 'authorization_code'}, | |
access_token_method='POST' | |
) | |
@app.route('/') | |
def login(): | |
return campaign_monitor.authorize( | |
callback=url_for('oauth_authorized', _external=True)) | |
@app.route('/oauth_authorized') | |
@campaign_monitor.authorized_handler | |
def oauth_authorized(resp): | |
access_token = resp['access_token'] | |
refresh_token = resp['refresh_token'] | |
expires_in = resp['expires_in'] | |
response = "<pre>" | |
response += "Your user is successfully authenticated. Here are the details you need:<br/><br/>" | |
response += "access token: %s<br/>" % access_token | |
response += "refresh token: %s<br/>" % refresh_token | |
response += "expires in: %s<br/>" % expires_in | |
response += "<br/><br/>" | |
cs = CreateSend() | |
cs.auth({ | |
'access_token': access_token, | |
'refresh_token': refresh_token }) | |
clients = cs.clients() | |
response += "We've made an API call too. Here are your clients:<br/><br/>" | |
for c in clients: | |
response += "%s (%s)<br/>" % (c.Name, c.ClientID) | |
response += "</pre>" | |
return response | |
if __name__ == '__main__': | |
port = int(os.environ.get('PORT', 5000)) | |
app.run(host='0.0.0.0', port=port, debug=True) |
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: python app.py |
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
Flask==0.9 | |
Flask-OAuth==0.12 | |
Jinja2==2.6 | |
Werkzeug==0.8.3 | |
createsend | |
httplib2==0.7.7 | |
oauth2==1.5.211 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment