gpt4 book ai didi

php - Facebook 访问 token - 服务器端身份验证

转载 作者:可可西里 更新时间:2023-11-01 12:50:54 24 4
gpt4 key购买 nike

你好,我正在尝试使用 facebook 服务器端身份验证来获取访问 token ,但不断向我抛出错误。

我正在使用波纹管:

$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 = "https://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.");
}

App ID 和 APP Secret 已经被移除所以不用担心那部分。我收到有关 get_file_contents 超时的错误消息。

[function.file-get-contents]: failed to open stream: Connection timed out

是否有其他或更好的方法来获取访问 token ?

Bellow 是我尝试过的版本,包括 cURL 想法:

function get_data_cURL($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

function getAccessToken(){
$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$canvas_URL = FB_CANVAS_URL;

session_start();
$code = $_REQUEST["code"];

if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_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($canvas_URL)
. "&client_secret=" . $app_secret . "&code=" . $code;

$returned_content = get_data_cURL($token_url);
echo'Well Done! - '.$returned_content;

}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
}

详细信息:

应用程序设置 - Canvas URL:http://www.apps.elistone.co.uk/DrawMyFriend/

FB_APP_ID = The Facebook App ID
FB_SECRET_ID = The Facebook Secret ID
FB_CANVAS_URL = http://www.apps.elistone.co.uk/DrawMyFriend/

即使有了这个更新,我仍然会收到这个错误:

旧错误

Well Done! - {"error":{"message":"Error validating verification code.","type":"OAuthException","code":100}}

新错误

这似乎是由重定向删除状态代码引起的。

The state does not match. You may be a victim of CSRF. - 193c791ddd71c3fd84d411db5554c315 (state code)

我还尝试通过更改绕过 CSRF 保护:

if($_REQUEST['state'] == $_SESSION['state'])

收件人:

 if(!empty($code))

但这仍然是旧错误问题,我听说这是由重定向链接中的某些内容引起的,但我不确定如何修复它。

问题已解决

经过一段时间的尝试,我已经使用 PHP SDK 使用以下方法获取用户访问 token :

$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$canvas_URL = FB_PAGE_URL;
$code = $_REQUEST["code"];

if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . $canvas_URL . "&state="
. $_SESSION['state'];

echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$facebook->api('oauth/access_token', array(
'client_id' => FB_APP_ID,
'client_secret' => FB_SECRET_ID,
'type' => 'client_cred',
'code' => $code,
));
$token = $facebook->getAccessToken();
echo$token;
}

我现在要把它变成某种功能。非常感谢大家的时间和帮助。

希望这对将来有需要的人有帮助(除非 Facebook 突然改变一切)

最佳答案

我推荐你使用PHP SDK library用于与 Facebook API 通信。只需使用 getAccessToken()getAccessTokenFromCode() 方法即可。

更新

要检索用户访问 token ,您必须在获取访问 token 之前发送 oauth 请求。答案更新中的详细信息:

    $facebook->api('oauth/access_token', array(
'client_id' => APP_ID,
'client_secret' => APP_SECRET,
'type' => 'client_cred',
'code' => $code,
));
$token = $facebook->getAccessToken();

关于php - Facebook 访问 token - 服务器端身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10833134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com