0

我尝试与 Facebook connect 建立连接,但出现了一些错误。

进一步的 Facebook 登录 Javascript 错误

我对这个链接有类似的问题。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 没有创建?

4

1 回答 1

0

您使用旧的 oauth 1.0 格式。您现在必须使用新格式。

一个来自 PHP 代码的示例,它应该足够接近,以最小的更改修改为 js:

函数 get_facebook_cookie() { $CI =& get_instance(); $app_id = $CI->config->item('facebook_app_id'); $application_secret = $CI->config->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;
 }

}

这来自这篇有用的博客文章以及有关该问题的其他一些花絮。

于 2013-09-12T13:55:57.813 回答