Last active
July 13, 2016 18:31
-
-
Save jonbesga/2c4e21ec7ed02901ad533f8d62885ae8 to your computer and use it in GitHub Desktop.
Basic Facebook OAuth implementation for Flask
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
APP_ID = YOUR_APP_ID | |
REDIRECT_URI = 'http://localhost:5000/callback/facebook' | |
SCOPE = 'public_profile,email' | |
APP_SECRET = YOUR_APP_SECRET |
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
@app.route('/authorize/facebook') | |
def fb_authorize(): | |
url = 'https://www.facebook.com/dialog/oauth?client_id={0}&redirect_uri={1}&scope={2}'.format( | |
app.config['APP_ID'], app.config['REDIRECT_URI'], app.config['SCOPE']) | |
return redirect(url) | |
@app.route('/callback/facebook') | |
def fb_callback(): | |
code = request.args.get('code') | |
url = 'https://graph.facebook.com/v2.3/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}'.format( | |
app.config['APP_ID'], app.config['REDIRECT_URI'], app.config['APP_SECRET'], code) | |
response = requests.get(url) | |
session['access_token'] = response.json()['access_token'] | |
return str('Correcto') | |
@app.route('/fb/me/') | |
def fb_me(): | |
response_me = requests.get('https://graph.facebook.com/v2.5/me?fields=id,name,email', | |
headers={'Authorization': 'OAuth {0}'.format(session['access_token'])}) | |
return str(response_me.json()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment