我尝试与Facebook连接建立连接,但有一些错误。
Further Facebook Login Javascript Errors
我有这个链接的类似问题.Php $ _COOKIE没有找到相关的cookie
Notice: Undefined index: fbs_appID in /opt/lampp/htdocs/phpdeneme/index.php on line 10
Notice: Undefined index: sig in /opt/lampp/htdocs/phpdeneme/index.php on line 22
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 /opt/lampp/htdocs/phpdeneme/index.php on line 30
<?php
define('FACEBOOK_APP_ID', 'APP_ID');
define('FACEBOOK_APP_SECRET','SECRET_ID');
function get_facebook_cookie($appID,$appSecret){
$args = array();
parse_str(trim($_COOKIE['fbs_' . $appID], '\\"'), $args);
ksort($args);
$payload = '';
foreach($args as $key=>$value){
if($key != 'sig'){
$payload .= $key . '=' . $value;
}
}
if(md5($payload . $appSecret) != $args['sig'])
return null;
return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
$user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $cookie['access_token']));
?>
<html>
<body>
<?php if($cookie){ ?>
Welcome <?= $user->name ?>
<?php } else { ?>
<fb: login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>' , status: true, cookie: true, xfbml:true});
FB.Event.subscribe('auth.login',function(response){
window.location.reload();
});
</script>
</body>
</html>
怎么了?少了什么东西 ?什么fbs cookie没有创建?
答案 0 :(得分:0)
您使用旧的oauth 1.0格式。您现在必须使用新格式。
来自PHP代码的一个例子,应该足够接近,以最小的变化修改成js:
function get_facebook_cookie() { $ CI =&amp; get_instance(); $ app_id = $ CI-&gt; config-&gt; item('facebook_app_id'); $ application_secret = $ CI-&gt; config-&gt; item('facebook_app_secret'); if(isset($ COOKIE ['fbsr '。$ app_id])){ list($ encoded_sig,$ payload)= explode('。',$ COOKIE ['fbsr '。$ app_id],2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
return null;
}
$expected_sig = hash_hmac('sha256', $payload,
$application_secret, $raw = true);
if ($sig !== $expected_sig) {
return null;
}
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&client_secret=" . $application_secret. "&redirect_uri=" . "&code=" . $data['code'];
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$data['access_token'] = $params['access_token'];
return $data;
}else{
return null;
}
}
这来自useful blog post以及其他一些关于这个问题的花絮。