Monthly Archives: May 2016


OAuth is an open standard used as a way for people to log into third party sites through other accounts such as Facebook, Google, Twitter, Linkedin, etc.

I got the idea to have a Facebook or Google log-in through (a site I was using to learn web development). As a user, I really liked that creating an account was easy and also that I didn’t have to remember a username and password.

While I was traveling, I decided to work on a project that would allow people to look for other people to play ping pong with. I wanted to manage the user creation and login through OAuth.

I was finally able to get the authorization working by using Flask-OAuthlib. It has Google and Facebook examples on its Github. I did have to start using Python 2.7.

In order to use Oauth for Facebook and Google, you need to register an app and get an ID and SECRET. You use these along with the appropriate urls to create OAuth objects that you can later use to access authorized user information.


from flask_oauthlib.client import OAuth

google = oauth.remote_app(
	  'scope': 'email'

facebook = oauth.remote_app(
	request_token_params={'scope': 'email'},

When the user clicks on the Google/Facebook Login, they are taken to a Google or Facebook page and are asked if they want to grant the third-party client (my app) permission to access the specified information. In my case, I was just requesting the name and email. If the request goes through fine and permission is granted, the app will be able to access this information which will be stored in the ‘me’ object.


from app import app, db, google, facebook, lm

def login(server_name):
    if server_name == "Facebook":
        callback = url_for(
                or request.referrer 
                or None,
        return facebook.authorize(callback=callback)

    return google.authorize(
        callback=url_for('g_authorized', _external=True)

def facebook_authorized():
    resp = facebook.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
    if isinstance(resp, OAuthException):
        return 'Access denied: %s' % resp.message

    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get(
    return set_user('Facebook', me)

def get_facebook_oauth_token():
    return session.get('oauth_token')

def g_authorized():
    resp = google.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
    session['google_token'] = (resp['access_token'], '')
    me = google.get('userinfo')
    return set_user('Google', me)

def get_google_oauth_token():
    return session.get('google_token')

I then use this information to either create a new account or log my user in.

def create_user(me, auth_server_name):  
    if auth_server_name == 'Facebook':
        profile_url =['picture']['data']['url']
        profile_url =['picture']
    new_user = User(

    login_user(new_user, remember=True)
    return new_user

def set_user(server_name, me):
    user = User.query.filter_by(
    if user is None:
        user = create_user(me, server_name)
        return redirect(url_for('set_location'))

    login_user(user, remember=True)
    return redirect(url_for('find_game'))

The full code for my project is on my github.

Finished Traveling

We are finally back home. We started our travels on April 11th and got back on May 24th. We visited Thailand (Bangkok, Chiang Mai, Krabbi), Singapore, Hong Kong, Japan (Kyoto and Tokyo) and Seoul.

While I didn’t do much writing I did continue learning to code and worked on this project. ItsPong is a web app that helps users find nearby people interested in playing ping pong.

I also started learning about how to use Backbone.js and am in the process of thinking about a single-page web app project to work on.

I primarily keep this blog because I believe reiterating what I’m learning forces me to understand things better.

I’ll keep it short for today. My previous routine is completely decimated at this point and I might need a few days to ease back into it.