Wednesday, July 13, 2011

facebook application php SDK: Automatic Authorization

when user of facebook open your application for the first time, the SDK will authenticate the user and request for authorization.

the typical SDK suggestion is:

redirection method is up to the programmer. From the documentation, they use the login button or a link to the login page. The login url was constructed using the SDK function getLoginUrl().

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   echo "<a href='" . $loginUrl . "'>login</a>";
}

So user must click the login hyperlink. But using header function from PHP, we can change the code:

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   header("Location: $loginUrl") //remember to use double quote as we enclosed a variable
}

this code work fine, until you use it in facebook application canvas. In the canvas, it will redirect you to the facebook logo, and again user must click the "go to facebook" link.

I think there is no serverside solution for this, so our option is javascript:

if ($user) {
    $logoutUrl = facebook->getLogoutUrl();
} else {
   $loginUrl = facebook->getLoginUrl();
   echo "<script> top.location.href='" . $loginUrl . "'</script>";
}

this will entirely redirect the browser to the login or authorization dialog. So you think we done already. Not quite...

No comments:

Post a Comment