facebook - 如果我们使用 JS SDK 登录,则在服务器端获取 facebook session

标签 facebook authentication

friend ,

我已经使用 JS SDK 为我的网站设置了 facebook 登录。

  1. 如果使用是通过JS SDK登录的,我们是否应该在服务器端交叉验证 session 是否有效,因为客户端很容易被伪造。

  2. 由于我使用 JS SDK,服务器将无法访问 facebook session 。 如果我需要在服务器端验证 session ,我可以像在 https://developers.facebook.com/docs/reference/php/ 中指定的那样使用 php-sdk 和 session 外部吗? ?在这种情况下,我需要启用 CURL PHP 扩展来运行它,并且担心在使用 php sdk 时性能是否会下降。

你能帮我找到上述问题的答案吗?

最佳答案

php sdk 和 javascript 完全相反,正如 Julian H. Lam 所说,实际上它们是为一起使用而构建的。

关于 php sdk documentation你可以找到这个:

Integration with the Facebook SDK for JavaScript

Used in conjunction with the Facebook SDK for JavaScript, the PHP SDK can share user sessions seamlessly across the client and server. If a user is logged in with Facebook and has authorized your app, the JavaScript SDK can pick up the user session persist this in a cookie which, which the PHP SDK reads without any intervention on the developer's part.

To enable this functionality, ensure that when you embed and initialise the JS SDK, you set both the status and the cookie parameters of the object passed to FB.init() to true.

通过使用基本逻辑,这很有意义,在客户端,您可以创建监听器来检索用户状态(如果他已登录,如果他已授予权限,如果他已注销),在服务器端根本没有任何意义。

所以我对你的建议是使用 Javascript SDK 来处理用户事件,就像我之前提到的那样,并处理来自用户操作的响应,比如当用户点赞或分享帖子时使用提要对话等 使用 php SDK,您只需检查您是否拥有有效用户,因为在使用 javascript SDK 处理登录过程后,您正在为客户端和服务器端共享相同的 cookie,如果您这样做 $ fb_id = $facebook->getUser() (当然是在初始化PHP SDK之后),你会得到用户facebook id,现在你知道你有一个有效的用户,你可以使用PHP SDK查询有关用户的信息,代表用户发帖等。

这是一个正确加载带有 cookie 支持的 javascript SDK 的示例:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'YOUR_APP_ID',                        // App ID from the app dashboard
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true,                                  // Look for social plugins on the page
      cookie     : true
    });

    // Additional initialization code such as adding Event Listeners goes here
  };

  // Load the SDK asynchronously
  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

这是服务器端的一个简单代码,只是为了启发您:

require_once("facebook.php");

$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional

$facebook = new Facebook($config);
try {
    $user_profile = $facebook->api('/me','GET');
    $user_name = $user_profile['name'];
    $user_email = $user_profile['email'];

} catch(FacebookApiException $e) {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
}

PS:此服务器端代码仅在用户已授予范围电子邮件权限时才有效

关于facebook - 如果我们使用 JS SDK 登录,则在服务器端获取 facebook session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15140130/

相关文章:

facebook 应用命名空间和 facebook 页面名称

iphone - 如何使用FBConnect对话框方法将UIImage发布到我的墙上?

c++ - 通过 boost 和 c++ 进行 smtp 身份验证

c# - Windows 应用程序的身份验证机制

Laravel 5 如何在访客/经过身份验证的用户上显示 Blade 内容?

java - session 无效后,如何强制Jetty使用BASIC身份验证要求凭证?

angular 5 - 登录后重定向上一页

java - 通过应用程序在 Facebook 粉丝页面上发布

ios - iTunes 存储操作失败应用程序在负载 : setResult; taskWithResult 中引用非公共(public)选择器

java - 如果作为现有 Android 源代码导入,Facebook SDK 会导致 google-play-services-lib 崩溃