Managing Facebook Authentication for login/out and asking permissions from users
In the past 3 parts of these tutorials, I talked about how Facebook mobile SDK works in general and how you can create a new Facebook app in the Facebook developer console and also you learned how to set your Facebook app to correctly connect to your Adobe Air app. In this post I am going to talk about Facebook authentication and how you can use the Facebook SDK ANE to ask permissions from your app users. everything related to managing the login API is managed through FB.logManager property.
When I use the word login here, I actually mean that you are asking your app users for some permissions. these permissions are a list of access request to their personal information available on their Facebook account and based on their decision, you can manage your app logic. This means that your users will know what permissions you are asking from them and they can decide if they like to grant your app with these permissions or they can simply skip them. in both cases, you will know their decision and you can know which permissions they have accepted and which ones they have declined.
// call this method only once in your app loading up
FB.getInstance("000000000000000"); // your Facebook app ID
// add listeners to know the result of your login process
// login like this
FB.logManager.requestPermission(LogManager.WITH_READ_PERMISSIONS, Permissions.public_profile, Permissions.user_friends, Permissions.email);
As you see in the sample code above, you are calling FB.logManager.requestPermission method and pass in a list of permissions you need for your app. please make sure to study the list of permissions you can ask from your users in com.myflashlab.air.extensions.facebook.access.Permissions. The usual permissions required by most apps are Permissions.public_profile, Permissions.user_friends, Permissions.email but you can ask for many, many other permissions.
it’s very important to know when you are asking your users permissions. it’s very recommended not to scare your users at the beginning of your app by asking them a lot of permissions. we have built and optimized the FB.logManager.requestPermission method for you in a way which you can use it anytime you wish in your app. it means that you can ask for basic permissions at first and let your users feel your app better and then, when it is required, you can call this method again and add the new permissions.
Every time you call FB.logManager.requestPermission you need to pass in the list of all the permissions you need. if you remove a previously added permission, it means that you do not need that permission anymore! so, if you had asked for Permissions.public_profile and Permissions.user_friends before and now you want to add permission for email access, you must call the method with all the three permissions again.
It’s worthy to mention that although you can ask for many different personal permissions from your users, Facebook needs to review your app before it can allow you to use those permissions! you’ll be fine with the popular permissions like email, public info and friends but for other personal information, you might need Facebook green light. study the asDoc in com.doitflash.air.extensions.facebook.access.Permissions or check Facebook official doc about permissions here.
Alright, the final thing I need to point out in this post is about what should be your next step after successfully asking your users for the permissions because until now, you just asked your users if you can have access to their email address for example, but you still don’t know how to obtain the information you just asked for, right?
Facebook has a restful API which they call it the open Graph API. When you have your users logged into your app, a token will be generated and with that you can call on the graph API. we will talk more about Graph API on part 7.
trace("token = " + FB.logManager.token);
trace("granted permissions = " + FB.logManager.permissions);
trace("declined Permissions = " + FB.logManager.declinedPermissions);
Code above shows how you can check the permissions your users have granted your app and also show you how you can get the token id you may require for complex Graph API calls.