Using Anvil to create a Facebook app?

Has anyone used Anvil to create a Facebook app?

It seems like it’d be a great combination, but there are probably complications involved in integrating the Facebook Javascript SDK.

Anyone have an example they can share?

A question that might be relevant even if someone does have an example:

  1. Is the Facebook SDK already included in Anvil if I am using the Facebook login in the Anvil app? Or do I need to include the Facebook Javascript separately?

Yes, you can do that with Anvil, and we have customers who do!

Anvil doesn’t import the Facebook Javascript API, but you can specify arbitrary OAuth scopes by calling anvil.facebook.auth.login(['scope-name-here']) (and get the resulting access token with anvil.facebook.auth.get_user_access_token()), so it’s possible you won’t need to tangle with Javascript at all!

If you do want to use Facebook’s JS API, you’ll want to import it via Native Libraries, and use Anvil’s Python-to-JS-to-Python calling support to communicate with your Javascript code.

Is there a way to get access to the default Facebook public profile fields without using the Javascript API? The user that gets returned has just the standard Anvil user fields.

You’d need to use the Facebook access token (as returned by anvil.facebook.auth.get_user_access_token()) to hit the Facebook REST API to get that data, but you can certainly do it!

Thanks for the nudge in the right direction. I’ll play with that.

For the next person with the same question as me (and no clue about the Facebook API either), here’s what I did to pull a couple basic profile fields. I added the Facebook Service as normal for doing a Facebook login, then on the login button had this code:

def button_1_click(self, **event_args):
  user = anvil.users.login_with_facebook(remember=True)
        
  if user:
    result = anvil.http.request('https://graph.facebook.com/me/?fields=name,email&access_token='+anvil.facebook.auth.get_user_access_token(), json=True)
    self.label_1.text = result['name']

So it’s pretty easy to use the Facebook REST API to get information about the user.

3 Likes