Skip to content

Instantly share code, notes, and snippets.

@chucknado
Last active January 20, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chucknado/b688cf139239aefa069f to your computer and use it in GitHub Desktop.
Save chucknado/b688cf139239aefa069f to your computer and use it in GitHub Desktop.
A Bottle application for a Zendesk App tutorial called "Add OAuth" at https://support.zendesk.com/hc/en-us/articles/205225558
import os
from urllib.parse import urlencode
import requests
from bottle import route, redirect, request, response, template, run
@route('/auth/asana')
def asana_auth():
params = {
'response_type': 'code',
'redirect_uri': 'https://my-example-app.herokuapp.com/auth/handle_decision',
'client_id': 'your_client_id',
'state': request.query.state
}
url = 'https://app.asana.com/-/oauth_authorize?' + urlencode(params)
redirect(url)
@route('/auth/handle_decision')
def handle_decision():
if 'error' in request.query_string:
return request.query.error_description
# get access token
params = {
'grant_type': 'authorization_code',
'code': request.query.code,
'client_id': 'your_client_id',
'client_secret': 'your_client_secret',
'redirect_uri': 'https://my-example-app.herokuapp.com/auth/handle_decision'
}
url = 'https://app.asana.com/-/oauth_token'
r = requests.post(url, data=params)
if r.status_code != 200:
error_msg = 'Failed to get access token with error {}'.format(r.status_code)
return error_msg
else:
data = r.json()
response.set_cookie('sheet', data['access_token'], max_age=data['expires_in'])
response.set_cookie('clean_sheet', data['refresh_token'])
redirect('https://your_subdomain.zendesk.com/agent/tickets/{}'.format(request.query.state))
@route('/auth/user_token')
def get_cookies():
access_token = request.get_cookie('sheet')
refresh_token = request.get_cookie('clean_sheet')
if access_token:
token = access_token
elif refresh_token:
params = {
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'client_id': 'your_client_id',
'client_secret': 'your_client_secret',
'redirect_uri': 'https://my-example-app.herokuapp.com/auth/handle_decision'
}
url = 'https://app.asana.com/-/oauth_token'
r = requests.post(url, data=params)
if r.status_code != 200:
error_msg = 'Failed to get access token with error {}'.format(r.status_code)
return error_msg
else:
data = r.json()
response.set_cookie('sheet', data['access_token'], max_age=data['expires_in'])
token = data['access_token']
else:
token = 'undefined'
return template('auth', token=token)
if os.environ.get('APP_LOCATION') == 'heroku':
run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
else:
run(host='localhost', port=8080, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment