views:

1060

answers:

1

I'm trying to get my FBML canvas page to automatically prompt new app users for permission to publish_stream. Following Facebook's documentation I tried using the required_permissions argument to require_login. That is, I tried to use the pyfacebook require_login decorator like this:

@facebook.require_login(required_permissions='publish_stream')

as in:

@decorator_from_middleware(FacebookMiddleware)
@facebook.require_login(required_permissions='publish_stream')
def canvas(request, template):
   ...

Requesting extended permissions in a pyfacebook-based Facebook iFrame app has been discussed. Requesting extended permissions in an FBML app too. My objective is to require extended permissions in an FBML app. Am I missing something or can anyone suggest a workaround?

Thanks.

+2  A: 

Right, pyfacebook does not yet support required_permissions for login, but you can call (or rather, redirect to) authorize manually. I've just written the following decorator for my purposes:

def require_permissions(*names):
    """Require extended permissions.
    XXX: in theory, the facebook.require_login() decorator should support
    this, but doesn't in pyfacebook yet."""
    def decorator(fn):
        required_perms = set(names)
        def wrapper(request, *args, **kwargs):
            fb = request.facebook
            perms = set() if fb.ext_perms is None \
                    else set(fb.ext_perms.split(','))
            if not required_perms.issubset(perms):
                missing = required_perms.difference(perms)
                url = fb.get_ext_perm_url(','.join(missing),
                            next='%s%s' % (fb.get_app_url(), request.path[1:]))
                print url
                return fb.redirect(url)
            return fn(request, *args, **kwargs)
        return wrapper
    return decorator

You may have to tweak the value of next as it is passed to get_ext_perm_url, but this should otherwise be fine (works for me). Usage example:

@facebook.require_login()
@facebook.require_add()
@require_permissions('email', 'offline_access')
def index(request):
   # ...
admp
Works like a charm. Great solution admp. As you suggested, I had to tweak the value for next. fb.get_app_url() was returning a URL with "None", maybe because fb.app_name was not set. I went with next = '%s' % (FACEBOOK_APP_URL) and defined this setting var myself.
jlpp
If I remember correctly, I had to define `FACEBOOK_APP_NAME` in my settings, to get `get_app_url` working.
admp
Good tip. Thanks again.
jlpp
There's a pyfacebook fork over at github that now includes this thing out of the box: http://github.com/tubaman/pyfacebook/See the corresponding commit here: http://github.com/tubaman/pyfacebook/commit/4043a78c25439586614219d8c8fac3fd7c8ea8a8However, I've grown highly disappointed by the situation with pyfacebook -- the author has sort of abandoned the project, there are tens of diverged forks etc. I am onto using the new OpenGraph API where possible now.
admp
admp:thanks for this, i tried this, but it prompts for permission one by one, and not like a unified ask permission way. for example hotlist does it like thishttp://img196.imageshack.us/img196/6968/hotlist.pngis there anyway to get this prompt/page for getting permissions with pyfacebook and fbml apps?
kevin