How should I go about redirecting the user back to my page\'s tab after they authenticate my app? I cannot put one specific url in for the redirect since my app will live on
So when a Page add/install your app, you should store the page's link
along with the page's id
.
Now when your page tab is loaded, Facebook will send the page
parameter which will contain the page id
(along with other info, refer to the documentation). You retrieve that id, get the page's link from your db and construct your page tab link, which would be something like (where $page
is the page's db record):
$redirect_uri = $page['page_link'] . '?sk=app_' . $APP_ID
Since you are using the PHP-SDK, this is how you construct your login:
$user = $facebook->getUser();
if(!$user) {
$url = $facebook->getLoginUrl(array('redirect_uri' => $redirect_uri, 'scope' => 'read_stream'));
echo "<script>top.location.href = '$url';</script>";
exit;
}
Of course you may not want to redirect to the login directly but instead have a call to action link:
<a href="<?php echo $url;?>" target="_top">Connect</a>
Best way I found is to set the Site URL in the app settings to http://www.facebook.com/pages/
Then do something like this(tested):
$uid = '';
$facebook = new Facebook(array(
'appId' =>'xxxxxxxxxxxxxxx',
'secret' =>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
'oauth' => true,
));
$uid = $facebook->getUser();
$signedrequest = $facebook->getSignedRequest();
$page_id = $signedrequest["page"]["id"];
$fb_access_token = $signedrequest["oauth_token"];
if($uid==''){
echo '<div id="authenticate"><a href="https://www.facebook.com/dialog/oauth/?client_id=APP_CLIENT_ID_HERE&redirect_uri=http://www.facebook.com/pages/'. $page_id .'/'.$page_id.'%3Fsk%3Dapp_APP_CLIENT_ID_HERE&response_type=token" target="_top">Authorize App</a></div>';
This might be a little bit of a hack, but it works well for my situation:) Hope it helps someone!