Facebook PHP-SDK没有';t处理通过$\u GET传递的代码/状态参数?
它看起来像Facebook发送Facebook PHP-SDK没有';t处理通过$\u GET传递的代码/状态参数?,php,facebook-php-sdk,oauth-2.0,Php,Facebook Php Sdk,Oauth 2.0,它看起来像Facebook发送code时的身份验证位,state通过$\u GET发送参数在PHP-SDK中没有涉及 if(!empty($_GET['code']) && !empty($_GET['state'])) { $response = file_get_contents('https://graph.facebook.com/oauth/access_token?' . http_build_query(array('client_id' => A
code
时的身份验证位,state
通过$\u GET发送参数在PHP-SDK中没有涉及
if(!empty($_GET['code']) && !empty($_GET['state']))
{
$response = file_get_contents('https://graph.facebook.com/oauth/access_token?' . http_build_query(array('client_id' => AY_FACEBOOK_APP_ID, 'client_secret' => AY_FACEBOOK_APP_SECRET, 'redirect_uri' => AY_FACEBOOK_TAB_URL, 'code' => $_GET['code'])));
// now check state and parse access token
ay($response);
}
我忽略了什么吗?如果没有,那么不包括它的原因是什么
请注意,我并没有像DMCS和Luc Franken那样要求提供一个示例
$response = file_get_contents(
'https://graph.facebook.com/oauth/access_token?' . http_build_query(
array(
'client_id' => AY_FACEBOOK_APP_ID,
'client_secret' => AY_FACEBOOK_APP_SECRET,
'redirect_uri' => AY_FACEBOOK_TAB_URL,
'code' => $_GET['code']
)
)
);
// now check state and parse access token
ay($response);
读起来好一点
现在你的问题是:它只是工作:
使用此测试代码:
echo 'https://graph.facebook.com/oauth/access_token?' . http_build_query(
array(
'client_id' => 1,
'client_secret' => 2,
'redirect_uri' => 3,
'code' => '1234'
)
);
尝试将url放在一个变量中,这将使调试更容易
如果在code=部分中没有得到任何内容,那么$\u get['code']变量中可能有值,http\u build\u查询将不接受该值,因为该函数URL对数组数据进行编码。是的,状态和代码参数将在关于CSRF保护的部分中讨论
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($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) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$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));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
您询问是否记录了执行状态的原因。我不仅为您提供了文档的链接,还发布了他们的代码片段,其中包含了这些信息。道歉,因为代码不太可读,所以直接从代码开始,经过3次阅读,现在得到了线索。请:)@DMCS,不,我从来没有问过记录状态(未)的原因。
我问过为什么PHP-SDK中没有包含此代码逻辑。可能是疏忽,或者在源代码管理合并中丢失了。您可以将其记录为缺陷