-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10de3de
commit 6aeaa68
Showing
6 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
google example | ||
~~~~~~~~~~~~~~ | ||
This example is contributed by Bruno Rocha | ||
GitHub: https://github.com/rochacbruno | ||
""" | ||
from flask import Flask, redirect, url_for, session, request, jsonify | ||
from flask_oauthlib.client import OAuth | ||
|
||
app = Flask(__name__) | ||
app.config['GOOGLE_ID'] = "898576985793-1oaem52t1huruq0k1s6hirvc91ulpm5k.apps.googleusercontent.com" | ||
app.config['GOOGLE_SECRET'] = "dflBTM76uotZw6NZh0rhQP-z" | ||
app.debug = True | ||
app.secret_key = 'development' | ||
oauth = OAuth(app) | ||
|
||
google = oauth.remote_app( | ||
'google', | ||
consumer_key=app.config.get('GOOGLE_ID'), | ||
consumer_secret=app.config.get('GOOGLE_SECRET'), | ||
request_token_params={ | ||
'scope': 'email' | ||
}, | ||
base_url='https://www.googleapis.com/oauth2/v1/', | ||
request_token_url=None, | ||
access_token_method='POST', | ||
access_token_url='https://accounts.google.com/o/oauth2/token', | ||
authorize_url='https://accounts.google.com/o/oauth2/auth', | ||
) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
if 'google_token' in session: | ||
me = google.get('userinfo') | ||
return jsonify({"data": me.data}) | ||
return redirect(url_for('login')) | ||
|
||
|
||
@app.route('/login') | ||
def login(): | ||
return google.authorize(callback=url_for('authorized', _external=True)) | ||
|
||
|
||
@app.route('/logout') | ||
def logout(): | ||
session.pop('google_token', None) | ||
return redirect(url_for('index')) | ||
|
||
|
||
@app.route('/login/authorized') | ||
def authorized(): | ||
resp = google.authorized_response() | ||
if resp is None: | ||
return 'Access denied: reason=%s error=%s' % ( | ||
request.args['error_reason'], | ||
request.args['error_description'] | ||
) | ||
session['google_token'] = (resp['access_token'], '') | ||
me = google.get('userinfo') | ||
return jsonify({"data": me.data}) | ||
|
||
|
||
@google.tokengetter | ||
def get_google_oauth_token(): | ||
return session.get('google_token') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |