Javascript: Function that returns status for specified Facebook API permission

I’m working on an application where I want to use Facebook user authorization.

Being someone who is wary of over-granting permission to applications there are permissions I only want to ask for if the user is going to do something where they’re needed.

One of these is the friends list.  As the application will allow the user to invite other people as administrators and managers for their particular group,  I only want access to the friends list if this is something they want to do.

I came up with the following function to do the check for a specific permission.

Its important to remember that the user must grant you permission to access their profile,  and are logged in prior to using this function.

First you must ensure that you do initialize your Facebook connection using your application ID.


window.fbAsyncInit = function() {
FB.init({
appId : 'your app id here',
xfbml : true,
version : 'v2.2'
});

You can get your Facebook App ID here.

In testing, I called it after getting a connected status response from FB.getLoginStatus.

This is the code that calls the function.

This function is synchronous!  That means the script will NOT wait for it to return a value.

….so call it and store the values on initialization, or page load….or before you need it.


FB.getLoginStatus(function(response) {
if (response.status === 'connected') {

FB.api('/me', function(medata) {

//do stuff here

});
checkfbpermission('email');
});
}
});

If you’re wondering what the medata object looks like…here it is:

{"id":"myfbidnumber",
"email":"david.rothbaueratmyemail",
"first_name":"David",
"gender":"male",
"last_name":"Rothbauer",
"link":"https://www.facebook.com/app_scoped_user_id/myidnumber/",
"locale":"en_US",
"name":"David Rothbauer",
"timezone":-4,"updated_time":"2015-04-27T21:01:49+0000",
"verified":true}

Now, the function itself:

function checkfbpermission(perm) {
 FB.api('/me/permissions',function(data){

 /*
I loop through the child objects permission and status sequentially, the found boolean will tell me when to return the status value
*/

 var found = false;  
 
 for (var k in data) {
 
 console.log(JSON.stringify(data[k])); //this is useful for checking that the permission that you're looking for actually exists!
 
 for (var i=0;i<data[k].length;i++)
 
 for (var x in data[k][i]) {
 
 if (found == true) {
  return data[k][i][x];
  } 
 
 if (x == "permission" &&
 data[k][i][x] == perm) {

 //this will tell the script to return the value of the next object (status)
 found = true;
 
 }
 else {
 
 found = false;
 }
 }
 }
 return 'no data';
 
 });

I’m still working out how to best leverage Facebook user integration into my application. I will post more solutions that I think might be helpful as I develop them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s