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
Showing
2 changed files
with
69 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from flask import Flask, url_for, session, request, jsonify | ||
from flask_oauthlib.client import OAuth | ||
|
||
|
||
CLIENT_ID = 'GbRmKgbSMmlE2NlugMeFfQIba8hoVyBFsWS8Igsq' | ||
CLIENT_SECRET = 'BfP7jsN8dSsXjGLfTTPiEvarMJOpkZQ2Y7IVVee8X929LfolMV' | ||
|
||
|
||
app = Flask(__name__) | ||
app.debug = True | ||
app.secret_key = 'secret' | ||
oauth = OAuth(app) | ||
|
||
remote = oauth.remote_app( | ||
'remote', | ||
consumer_key=CLIENT_ID, | ||
consumer_secret=CLIENT_SECRET, | ||
request_token_params={'scope': 'email'}, | ||
base_url='http://127.0.0.1:5000/api/', | ||
request_token_url=None, | ||
access_token_url='http://127.0.0.1:5000/oauth/token', | ||
authorize_url='http://127.0.0.1:5000/oauth/authorize' | ||
) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
if 'remote_oauth' in session: | ||
return session['remote_oauth'][0] | ||
next_url = request.args.get('next') or request.referrer or None | ||
return remote.authorize( | ||
callback=url_for('authorized', next=next_url, _external=True) | ||
) | ||
|
||
|
||
@app.route('/authorized') | ||
@remote.authorized_handler | ||
def authorized(resp): | ||
if resp is None: | ||
return 'Access denied: reason=%s error=%s' % ( | ||
request.args['error_reason'], | ||
request.args['error_description'] | ||
) | ||
print resp | ||
session['remote_oauth'] = (resp['access_token'], '') | ||
return jsonify(oauth_token=resp['access_token']) | ||
|
||
|
||
@remote.tokengetter | ||
def get_oauth_token(): | ||
return session.get('remote_oauth') | ||
|
||
|
||
if __name__ == '__main__': | ||
import os | ||
os.environ['DEBUG'] = 'true' | ||
app.run(host='localhost', port=8000) |