application.py
def fbconnect():
    if request.args.get('state') != login_session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    access_token = request.data
    print "access token received %s " % access_token

    app_id = json.loads(open('fb_client_secrets.json', 'r').read())[
        'web']['app_id']
    app_secret = json.loads(
        open('fb_client_secrets.json', 'r').read())['web']['app_secret']
    url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % (app_id, app_secret, access_token)  # noqa
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]

    # Use token to get user info from API
    userinfo_url = "https://graph.facebook.com/v2.8/me"
    '''
        Due to the formatting for the result from the server token
        exchange we have to split the token first on commas and
        select the first index which gives us the key : value
        for the server access token then we split it on colons
        to pull out the actual token value and replace the
        remaining quotes with nothing so that it can be used
        directly in the graph api calls
    '''
    token = result.split(',')[0].split(':')[1].replace('"', '')

    url = 'https://graph.facebook.com/v2.8/me?access_token=%s&fields=name,id,email' % token  # noqa
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    # print "url sent for API access:%s"% url
    # print "API JSON result: %s" % result
    data = json.loads(result)
    login_session['provider'] = 'facebook'
    login_session['username'] = data["name"]
    login_session['email'] = data["email"]
    login_session['facebook_id'] = data["id"]

    # The token must be stored in the login_session in order to properly logout
    login_session['access_token'] = token

    # Get user picture
    url = 'https://graph.facebook.com/v2.8/me/picture?access_token=%s&redirect=0&height=200&width=200' % token  # noqa
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    data = json.loads(result)

    login_session['picture'] = data["data"]["url"]

    # see if user exists
    user_id = getUserID(login_session['email'])
    if not user_id:
        user_id = createUser(login_session)
    login_session['user_id'] = user_id

    output = ''
    output += '<h1>Welcome, '
    output += login_session['username']

    output += '!</h1>'
    output += '<img src="'
    output += login_session['picture']
    output += ' " style = "width: 300px; height: 300px;border-radius: 150px;'
    '"-webkit-border-radius: 150px;-moz-border-radius: 150px;"> '

    flash("Now logged in as %s" % login_session['username'])
    return output

        Due to the formatting for the result from the server token
        exchange we have to split the token first on commas and
        select the first index which gives us the key : value
        for the server access token then we split it on colons
        to pull out the actual token value and replace the
        remaining quotes with nothing so that it can be used
        directly in the graph api calls
    
Due to the formatting for the result from the server token
exchange we have to split the token first on commas and
select the first index which gives us the key : value
for the server access token then we split it on colons
to pull out the actual token value and replace the
remaining quotes with nothing so that it can be used
directly in the graph api calls
Due to the formatting for the result from the server token exchange we have to split the token first on commas and select the first index which gives us the key : value for the server access token then we split it on colons to pull out the actual token value and replace the remaining quotes with nothing so that it can be used directly in the graph api calls
def fbconnect():
    if request.args.get('state') != login_session['state']:
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    access_token = request.data
    print "access token received %s " % access_token
    app_id = json.loads(open('fb_client_secrets.json', 'r').read())[
        'web']['app_id']
    app_secret = json.loads(
        open('fb_client_secrets.json', 'r').read())['web']['app_secret']
    url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % (app_id, app_secret, access_token)  
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    userinfo_url = "https://graph.facebook.com/v2.8/me"
    token = result.split(',')[0].split(':')[1].replace('"', '')
    url = 'https://graph.facebook.com/v2.8/me?access_token=%s&fields=name,id,email' % token  
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    data = json.loads(result)
    login_session['provider'] = 'facebook'
    login_session['username'] = data["name"]
    login_session['email'] = data["email"]
    login_session['facebook_id'] = data["id"]
    login_session['access_token'] = token
    url = 'https://graph.facebook.com/v2.8/me/picture?access_token=%s&redirect=0&height=200&width=200' % token  
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    data = json.loads(result)
    login_session['picture'] = data["data"]["url"]
    user_id = getUserID(login_session['email'])
    if not user_id:
        user_id = createUser(login_session)
    login_session['user_id'] = user_id
    output = ''
    output += '<h1>Welcome, '
    output += login_session['username']
    output += '!</h1>'
    output += '<img src="'
    output += login_session['picture']
    output += ' " style = "width: 300px; height: 300px;border-radius: 150px;'
    flash("Now logged in as %s" % login_session['username'])
    return output
def fbconnect ( ) : if request . args . get ( 'state' ) != login_session [ 'state' ] : response = make_response ( json . dumps ( 'Invalid state parameter.' ) , 401 ) response . headers [ 'Content-Type' ] = 'application/json' return response access_token = request . data print "access token received %s " % access_token app_id = json . loads ( open ( 'fb_client_secrets.json' , 'r' ) . read ( ) ) [ 'web' ] [ 'app_id' ] app_secret = json . loads ( open ( 'fb_client_secrets.json' , 'r' ) . read ( ) ) [ 'web' ] [ 'app_secret' ] url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % ( app_id , app_secret , access_token ) h = httplib2 . Http ( ) result = h . request ( url , 'GET' ) [ 1 ] userinfo_url = "https://graph.facebook.com/v2.8/me" token = result . split ( ',' ) [ 0 ] . split ( ':' ) [ 1 ] . replace ( '"' , '' ) url = 'https://graph.facebook.com/v2.8/me?access_token=%s&fields=name,id,email' % token h = httplib2 . Http ( ) result = h . request ( url , 'GET' ) [ 1 ] data = json . loads ( result ) login_session [ 'provider' ] = 'facebook' login_session [ 'username' ] = data [ "name" ] login_session [ 'email' ] = data [ "email" ] login_session [ 'facebook_id' ] = data [ "id" ] login_session [ 'access_token' ] = token url = 'https://graph.facebook.com/v2.8/me/picture?access_token=%s&redirect=0&height=200&width=200' % token h = httplib2 . Http ( ) result = h . request ( url , 'GET' ) [ 1 ] data = json . loads ( result ) login_session [ 'picture' ] = data [ "data" ] [ "url" ] user_id = getUserID ( login_session [ 'email' ] ) if not user_id : user_id = createUser ( login_session ) login_session [ 'user_id' ] = user_id output = '' output += '<h1>Welcome, ' output += login_session [ 'username' ] output += '!</h1>' output += '<img src="' output += login_session [ 'picture' ] output += ' " style = "width: 300px; height: 300px;border-radius: 150px;' '"-webkit-border-radius: 150px;-moz-border-radius: 150px;"> ' flash ( "Now logged in as %s" % login_session [ 'username' ] ) return output
Due to the formatting for the result from the server token
exchange we have to split the token first on commas and
select the first index which gives us the key : value
for the server access token then we split it on colons
to pull out the actual token value and replace the
remaining quotes with nothing so that it can be used
directly in the graph api calls
Due to the formatting for the result from the server token exchange we have to split the token first on commas and select the first index which gives us the key : value for the server access token then we split it on colons to pull out the actual token value and replace the remaining quotes with nothing so that it can be used directly in the graph api calls
# noqa # Use token to get user info from API '''
        Due to the formatting for the result from the server token
        exchange we have to split the token first on commas and
        select the first index which gives us the key : value
        for the server access token then we split it on colons
        to pull out the actual token value and replace the
        remaining quotes with nothing so that it can be used
        directly in the graph api calls
    ''' # noqa # print "url sent for API access:%s"% url # print "API JSON result: %s" % result # The token must be stored in the login_session in order to properly logout # Get user picture # noqa # see if user exists '"-webkit-border-radius: 150px;-moz-border-radius: 150px;"> '