Question

I'm trying to use the pyfacebook functions (https://github.com/sciyoshi/pyfacebook/) in a Google app engine project. I've followed the advice on the Facebook developer forum (http://forum.developers.facebook.net/viewtopic.php?pid=164613) and added the additional functions to the __init__.py file, copied that file to the root directory of my project and renamed it facebook.py. Having imported facebook.py I added the following to the get(self) method for the Python class for the page:

facebookapi = facebook.Facebook(API_KEY, SECRET)

    if not facebookapi.check_connect_session(self.request):
        path = os.path.join(os.path.dirname(__file__), 'templates/login.html')
        self.response.out.write(template.render(path, {'apikey': API_KEY}))
        return

    user = facebookapi.users.getInfo(
        [facebookapi.uid],
        ['uid', 'name', 'birthday', 'relationship_status'])[0]

    template_values = {
        'name': user['name'],
        'birthday': user['birthday'],
        'relationship_status': user['relationship_status'],
        'uid': user['uid'],
        'apikey': API_KEY
    }

    path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
    self.response.out.write(template.render(path, template_values))

When running it I get the following error:

File "\much\baw08u\Private\IDS\helloworld\helloworld.py", line 54, in get

if not facebookapi.check_connect_session(self.request): AttributeError: 'Facebook' object has no attribute 'check_connect_session'

So it seems to be loading the facebook API fine, but not the new methods I've added. I copied and pasted the code from the developer forum at the bottom of the Facebook class definition, and made sure all the indentation was right but it still doesn't seem to be picking them up. Does anyone know what might be the problem?

Thanks

Ben

Was it helpful?

Solution

You believe the Facebook class has a certain method but Python is sure it hasn't. Why? Maybe you misspelled the method name, maybe you did not get the indentation right - hard to say without seeing the code.

You could try poking around to validate your assumptions:

import facebook
import logging

logging.warn('Facebook class: %r', dir(facebook.Facebook))
logging.warn('facebook module: %r', dir(facebook))

If you are sure you are operating on the correct file, the you should expect to see check_connect_session as a method of Facebook. If you didn't add enough indentation then you expect to see check_connect_method as a function defined in the facebook module. Too much indentation would make check_connect_method a sub function of which ever method precedes it and it won't show up in the above logging. Pay close attention to indentation.

However, a better way to add some custom methods might be:

import facebook

class Facebook(facebook.Facebook):
    def check_connect_session(request):
        pass

facebookapi = Facebook(API_KEY, SECRET)

if not facebookapi.check_connect_session(...):
    ...

Now when Facebook update their code you simply copy the new file into place - no need to merge your customisations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top