Skip to content

Instantly share code, notes, and snippets.

@lrvick
Created January 28, 2015 09:18
Show Gist options
  • Save lrvick/3d9ca3ed486c8e9b1473 to your computer and use it in GitHub Desktop.
Save lrvick/3d9ca3ed486c8e9b1473 to your computer and use it in GitHub Desktop.
Python/Flask oAuth authorization example.
from flask import Flask, request, redirect
from rauth.service import OAuth2Service
import simplejson as json
google = OAuth2Service(
name='google',
consumer_key='478110803143-ct2e336q06knns6p7pgnob1msandulj3.apps.googleusercontent.com',
consumer_secret='BGncEz1jPsNAMN7mzqki7zTy',
access_token_url='https://accounts.google.com/o/oauth2/token',
authorize_url='https://accounts.google.com/o/oauth2/auth'
)
port = 7000
ip = '0.0.0.0'
domain = 'localhost'
redirect_uri = 'http://%s:%s/' % (domain,port)
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
code = request.args.get('code', None)
if code is not None:
print 'CODE: ', code
response = google.get_access_token(
'POST',
data = {
'code' : code,
'redirect_uri' : redirect_uri,
},
)
access_token = response.content['access_token']
print 'ACCESS_TOKEN: ', access_token
return json.dumps(response.content)
return redirect('/login')
#return 'Hello, world!'
@app.route('/login')
def login():
authorize_url = google.get_authorize_url(
scope = 'https://www.googleapis.com/auth/userinfo.email \
https://www.googleapis.com/auth/userinfo.profile',
access_type='offline',
approval_prompt='force',
redirect_uri=redirect_uri
)
print authorize_url
return redirect(authorize_url)
if __name__ == '__main__':
app.run(host=ip,port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment