Monday 22 April 2013

Facebook app development, php SDK, ' $facebook->getuser returning 0 ' Problem

This has been a stubborn problem;
The problem being the user despite attempt to login by clicking the login url was not able to login, as the permission dialog box never appears to allow the user to approve the permission.

But try opening the login url in a new tab, it works !!.  For some reason the permission dialog box never appears in the same page.

So the work around is provided here.


The framework of login/logout looks like this


$facebook->getuser();

if($user)
{
//user logged in display the content
}


else
{
$url = "https://www.facebook.com/dialog/oauth?"
."client_id=CLIENTID&"
."redirect_uri=http://apps.facebook.com/APPNAMESPACE&"
."scope=COMMA_SEPARATED_PERMISSIONLIST";
echo '
<div>

<script> top.location.href="'.$url.'"</script>

</div>';
}


That's it folks.