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