Created
January 28, 2015 09:18
-
-
Save lrvick/3d9ca3ed486c8e9b1473 to your computer and use it in GitHub Desktop.
Python/Flask oAuth authorization example.
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
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