Last active
August 29, 2015 14:14
-
-
Save taylor224/c256adc22af209c2b38e to your computer and use it in GitHub Desktop.
Social Connect to Flask
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
# app/views/root.py | |
# -*- coding: utf-8 -*- | |
# Check Return data | |
# Facebook return data (https://developers.facebook.com/docs/reference/api/user/) | |
# Twitter Verify Credential return data (https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials) | |
from flask import ( | |
Blueprint, | |
redirect, | |
render_template, | |
session, | |
url_for, | |
request, | |
current_app, | |
send_file, | |
send_from_directory, | |
flash | |
) | |
import twitter | |
from flask_oauth import OAuth | |
reload(sys) | |
sys.setdefaultencoding("utf-8") | |
app = Blueprint('root', __name__) | |
oauth = OAuth() | |
mainurl = 'http://yourserver.com' # It is your server root url. Do not add '/' at the last. | |
# Social Token | |
# Facebook -> developer.facebook.com | |
# Twitter -> dev.twitter.com | |
FACEBOOK_APP_ID = '' | |
FACEBOOK_APP_SECRET = '' | |
TWITTER_CK = '' | |
TWITTER_CS = '' | |
facebook = oauth.remote_app('facebook', | |
base_url='https://graph.facebook.com/', | |
request_token_url=None, | |
access_token_url='/oauth/access_token', | |
authorize_url='https://www.facebook.com/dialog/oauth', | |
consumer_key=FACEBOOK_APP_ID, | |
consumer_secret=FACEBOOK_APP_SECRET, | |
request_token_params={'scope': ('email, ')} | |
) | |
twitterauth = oauth.remote_app('twitter', | |
base_url='https://api.twitter.com/1.1/', | |
request_token_url='https://api.twitter.com/oauth/request_token', | |
access_token_url='https://api.twitter.com/oauth/access_token', | |
authorize_url='https://api.twitter.com/oauth/authenticate', | |
consumer_key=TWITTER_CK, | |
consumer_secret=TWITTER_CS | |
) | |
############################# FACEBOOK ############################# | |
@app.route("/login/facebook_login") | |
def login_facebook_login(): | |
return facebook.authorize(callback= mainurl + '/login/facebook_authorized') | |
@app.route('/login/facebook_authorized') | |
@facebook.authorized_handler | |
def login_facebook_authorized(resp): | |
if resp is None: | |
return 'Access denied: reson=%s error=%s' % ( | |
request.args['error_reason'], | |
request.args['error_description'] | |
) | |
session['oauth_token'] = (resp['access_token'], '') | |
me = facebook.get('/me') | |
return "username = %s / name = %s / email = %s" % (me.data['username'], me.data['name'], me.data['email']) | |
@app.route("/signup/facebook_login") | |
def signup_facebook_login(): | |
return facebook.authorize(callback= mainurl + '/signup/facebook_authorized') | |
@app.route('/signup/facebook_authorized') | |
@facebook.authorized_handler | |
def signup_facebook_authorized(resp): | |
if resp is None: | |
return 'Access denied: reason=%s error=%s' % ( | |
request.args['error_reason'], | |
request.args['error_description'] | |
) | |
session['oauth_token'] = (resp['access_token'], '') | |
me = facebook.get('/me') | |
# If you want to know about all of facebook api return data, See README.md | |
return render_template("signup.html", idd=me.data['username'], name=me.data['name'], email=me.data['email']) | |
@facebook.tokengetter | |
def get_facebook_oauth_token(): | |
return session.get('oauth_token') | |
############################# FACEBOOK FIN ############################# | |
############################# TWITTER ############################# | |
@app.route('/login/twitter_login') | |
def login_twitter_login(): | |
return twitterauth.authorize(callback= mainurl + '/login/twitter_authorized') | |
@app.route('/login/twitter_authorized') | |
@twitterauth.authorized_handler | |
def login_twitter_authorized(resp): | |
if resp is None: | |
return 'Denied the request to sign in' | |
t = twitter.Twitter(auth=twitter.OAuth(resp['oauth_token'], resp['oauth_token_secret'], TWITTER_CK, TWITTER_CS)) | |
ud = t.account.verify_credentials() | |
return 'username = %s / id = %s / userid = %s ' % (ud['name'], resp['screen_name'], ud['id']) | |
@app.route('/signup/twitter_login') | |
def signup_twitter_login(): | |
return twitterauth.authorize(callback= mainurl + '/signup/twitter_authorized') | |
@app.route('/signup/twitter_authorized') | |
@twitterauth.authorized_handler | |
def signup_twitter_authorized(resp): | |
if resp is None: | |
return 'You denied the request to sign in' | |
t = twitter.Twitter(auth=twitter.OAuth(resp['oauth_token'], resp['oauth_token_secret'], TWITTER_CK, TWITTER_CS)) | |
ud = t.account.verify_credentials() | |
# If you want to know all of Twitter return data, See README.md & type command in your server "pydoc twitter" | |
return render_template("signup.html", idd=resp['screen_name'], name=ud['name']) | |
@twitterauth.tokengetter | |
def get_twitter_token(token=None): | |
return session.get('twitter_token') | |
############################# TWITTER FIN ############################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment