问题
It's weird because on my canvas page for my facebook app I get all these php errors about my auth_token and then it redirects and works as it should. Can someone help me figure this out por favor? Heres my php code at the top of the page:
$app_id = "181247432419054";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "https://apps.facebook.com/wellnessq/";
session_register();
session_start();
if (!isset($_REQUEST["code"]))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
$code = $_REQUEST['code'];
{
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
}
?>
The first error message starts at $code = $_REQUEST['code']; and then is followed by a few more. I cant post a screen shot because I have too few reputation points =/ grrr But here's the error messages:
Notice: Undefined index: code in D:\Extranet\www.mysite.com\manager\here\core.functions.php(663) : eval()'d code on line 19
Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?client_id=181247432419054&redirect_uri=https%3A%2F%2Fapps.facebook.com%2Fwellnessq%2F&client_secret=xxxxxxxxxxxxxxxxx&code=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:\Extranet\www.mysite.com\manager\here\core.functions.php(663) : eval()'d code on line 25
Notice: Undefined index: access_token in D:\Extranet\www.mysite.com\manager\here\core.functions.php(663) : eval()'d code on line 30
Warning: file_get_contents(https://graph.facebook.com/me?access_token=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:\Extranet\www.mysite.com\manager\here\core.functions.php(663) : eval()'d code on line 32
Thanks!
回答1:
You need to add an exit;
statement right after you echo the <script>
tag. I have noticed this oversight in a lot of the Facebook sample code. Normally it doesn't cause much problem but once you have determined that you need to redirect you should make sure you stop doing anything else and give the browser time to process the redirect script.
来源:https://stackoverflow.com/questions/7667941/auth-token-facebook-php-error