5/01/2012

Facebook - Check users' allowed permission for your app

In this entry, we will discover the answer of the question "How to check if the user has already allowed publish_stream for your app?". And luckily, we can achieve this goal by using Grap API. For example I will check whether the user has allowed publish_stream permission or not.
FB.api('/me/permissions', function (response) {
  //please note that we use data[0]
  if(response.data[0].publish_stream==1)
  {
     //Do some stuff such as post a photo to user wall        
  }
  else
  {
    alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
  }
}
It is easy, isn't it? There is my complete code for force user allow some permission for my app. It is not optimized yet, however it still run OK and meets my need.
FB.getLoginStatus(function(response) {
 if (response.status === 'connected') 
 {
  FB.api('/me/permissions', function (response) {
   if(response.data[0].publish_stream==1)
   {
    FB.api('/me', function(response) {
     
    });
   }
   else
   {
    FB.login(function(response) {
     FB.api('/me/permissions', function (response) {
      if(response.data[0].publish_stream==1)
      {
       //Do some stuff that need permissions
      }
      else
      {
       alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
      }
     });
    },
                                  {scope:'publish_stream,user_photos,user_birthday,email'});
   }
  });
 }
 else if (response.status === 'not_authorized')
 {
  FB.login(function(response) {
   if (response.authResponse) 
   {
    FB.api('/me/permissions', function (response) {
      if(response.data[0].publish_stream==1)
      {
      
      }
      else
      {
        alert('You need to grant "Post on your behalf" permission in order to save to Facebook!');
      }
    });
   } 
   else 
   {
    alert('You need to authorize the application before you do other action');
   } 
  }, {
   scope : 'publish_stream,user_photos,user_birthday,email'
  });
 }
});
Hung Nguyen

No comments:

Post a Comment

The 0/1 Knapsack Problem - Dynamic Programming

 References: https://www.youtube.com/watch?v=EH6h7WA7sDw  Class Item class Item { private $_weight; private $_value; public functi...