Я использую сочетание серверных и клиентских сценариев для достижения того, о чем вы просили. Предполагая, что PHP является вашим языком на стороне сервера, в большинстве случаев должно работать что-то вроде ниже:
<?php
$app_id = "<your app id>";
$page_url = "<your page url>";
// if your app requires extended permissions
$app_scope = "user_interests,publish_actions,publish_stream"; //etc.
// depending on your requirement you may use the oauth authentication
// or you may simply redirect to your page url instead
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($page_url)
. "&scope=" . $app_scope;
// this is passed only when your page is pulled by facebook
if (!isset($_REQUEST["signed_request"])) {
// we are sure that the page is not accessed within facebook
// so we may redirect
header ('Location: ' . $auth_url); // or $page_url
die();
}
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
// we can check this and redirect later
$user_id = $data["user_id"];
// other details are also avialable
$user = $data["user"];
$algorithm = $data["algorithm"];
$issued_at = $data["issued_at"];
$oauth_token = $data["oauth_token"];
$expires = $data["expires"];
// passed only when the app is loaded inside a page tab
// can be checked for and if empty we can redirect
$app_data = $data["app_data"];
// signed_request algorithm's signature verification
// is omitted here for simplicity sake. you may perform
// that to ensure the authenticity of the request
// other initial setups
?>
Теперь вы можете поместить следующий скрипт внутри тега <head> </head>
на своей странице:
<script type="text/javascript">
<?php if (empty($user_id)): // or as may the need be, you may want to check $app_data instead ?>
top.location.href="<?php echo $auth_url; // or you may want to use $page_url instead ?>";
</script>