Last active
December 12, 2015 10:48
-
-
Save jdennes/4761254 to your computer and use it in GitHub Desktop.
Another example Flask application to demonstrate authenticating with the Campaign Monitor API using OAuth. This example only uses the createsend and Flask 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, CREATESEND_CLIENT_SECRET, and CREATESEND_REDIRECT_URI | |
# 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 createsend import * | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = os.environ['SESSION_SECRET'] | |
@app.route('/') | |
def login(): | |
cs = CreateSend() | |
return redirect(cs.authorize_url( | |
os.environ['CREATESEND_CLIENT_ID'], | |
os.environ['CREATESEND_REDIRECT_URI'], | |
'ViewReports,CreateCampaigns,SendCampaigns' | |
)) | |
@app.route('/exchange_token') | |
def exchange_token(): | |
code = request.args.get('code', '') | |
cs = CreateSend() | |
access_token, expires_in, refresh_token = cs.exchange_token( | |
os.environ['CREATESEND_CLIENT_ID'], | |
os.environ['CREATESEND_CLIENT_SECRET'], | |
os.environ['CREATESEND_REDIRECT_URI'], | |
code | |
) | |
response = "<pre>" | |
response += "Your user is successfully authenticated. Here are you 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.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 | |
Jinja2==2.6 | |
Werkzeug==0.8.3 | |
createsend | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment