Skip to content

Instantly share code, notes, and snippets.

@dtenenba
Last active June 7, 2019 22:57
Show Gist options
  • Save dtenenba/4ea8331092aaa95036a1aa2b1737f675 to your computer and use it in GitHub Desktop.
Save dtenenba/4ea8331092aaa95036a1aa2b1737f675 to your computer and use it in GitHub Desktop.
a trivial python/flask app that uses Slack for sign-in

Set up as follows:

Install virtualenvwrapper if it isn't already installed:

pip install virtualenvwrapper

Create a virtualenv, using python 3:

mkvirtualenv --python $(which python3) flask-oauth

Install the requirements:

pip install -r requirements.txt

Set up your Slack application:

https://api.slack.com/docs/sign-in-with-slack

If needed, use ngrok to create a tunnel (and public https url) to your local app.

Save the python file below as runme.py and run it like this:

FLASK_APP=runme.py FLASK_DEBUG=1 flask run -p 8880

The python code refers to github throughout because it was stolen from a github example at https://github.com/lepture/flask-oauthlib/tree/master/example But it does not use github at all, it tries to use Slack.

It does not currently work. What is the secret sauce to make it work?

appdirs==1.4.3
appnope==0.1.0
click==6.7
decorator==4.0.11
Flask==0.12.1
Flask-OAuthlib==0.9.3
ipython==5.3.0
ipython-genutils==0.2.0
itsdangerous==0.24
Jinja2==2.9.5
MarkupSafe==1.0
oauthlib==2.0.2
packaging==16.8
pexpect==4.2.1
pickleshare==0.7.4
prompt-toolkit==1.0.14
ptyprocess==0.5.1
Pygments==2.2.0
pyparsing==2.2.0
requests==2.13.0
requests-oauthlib==0.8.0
simplegeneric==0.8.1
six==1.10.0
traitlets==4.3.2
wcwidth==0.1.7
Werkzeug==0.12.1
from flask import Flask, redirect, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
import requests
app = Flask(__name__)
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)
github = oauth.remote_app(
'github',
consumer_key='put your key here',
consumer_secret='put your secret here',
request_token_params={'scope': 'identity.basic'},
base_url='https://slack.com/api/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://slack.com/api/oauth.access',
authorize_url='https://slack.com/oauth/authorize?scope=identity.basic&client_id=put-your-key-here'
)
@app.route('/')
def index():
if 'github_token' in session:
me = github.get('user') # this causes an error, there is no such key
return jsonify(me.data)
return redirect(url_for('login'))
@app.route('/login')
def login():
return github.authorize(callback=url_for('authorized', _external=True))
@app.route('/logout')
def logout():
session.pop('github_token', None)
return redirect(url_for('index'))
@app.route('/login/authorized')
def authorized():
resp = github.authorized_response()
if resp is None or resp.get('access_token') is None:
return 'Access denied: reason=%s error=%s resp=%s' % (
request.args['error'],
request.args['error_description'],
resp
)
session['github_token'] = (resp['access_token'], '')
me = github.get('user') # this causes an error, there is no such key
return jsonify(me.data)
@github.tokengetter
def get_github_oauth_token():
return session.get('github_token')
if __name__ == '__main__':
app.run()
@brez
Copy link

brez commented Jun 7, 2019

Probably a little late here but I was using this as a ref and realized that you don't need the initial auth call if you're using the Sign in with Slack button because the Sign in Button does that part (I noticed it was making two requests) - i.e. just set /login/authorized as your callback url. Re. your error you can get what you need out of the response, e.g. me = resp['user']

@brez
Copy link

brez commented Jun 7, 2019

I think your error is simply the fact that github.get('user') should have been resp.get('user')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment