Python - Inicio de sesión global de Facebook

What I want to do in my Flask/Python app is similar to what I've done in PHP. In PHP, I was able to create a new facebook object and if the user had already authorised the app and was already logged in to their Facebook account, their information would be loaded into the object and they wouldn't have to click "Sign in with Facebook" on the site - it would automatically realise they're already logged in and authorised.

With my Python app, I'm using

def login():
    return facebook.authorize(callback=url_for('facebook_authorized',
        next=request.args.get('next') or request.referrer or None,

def facebook_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')

    usr = Users.query.filter_by(['id']).first()

    if (hasattr(usr, 'fb_id'))==False:
        # Find them in our database and log them in
        # Sign them up

    return redirect(url_for('home'))

Is there a way I can set it up so that the me object can be used throughout the application thus allowing automatic logins as I would be able to do this step upon page load without the user having to visit the login URL?

At the moment, the user has to visit /login in order to retrieve their Facebook information. Is it possible to do this like you can in PHP where it can be a variable used throughout the script? - Sorry for sounding redundant, I'm not sure how to explain this clearly.

In PHP, some example code would be like below - and this would be called on every page load, not requiring the user to visit the /login URL to login with their Facebook.


    $facebook = new Facebook(array(
      'appId'  => '',
      'secret' => '',

    $this->fbuser = $facebook->getUser();

    if ($this->fbuser) {
# etc..

preguntado el 01 de julio de 12 a las 02:07

1 Respuestas

Not too familiar with Flask, but it seems to me that logically, your unauthorized access handler could first attempt to log in the user using Facebook before rejecting their page load request.

Respondido 01 Jul 12, 02:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.