Javascript 在哪里可以找到 Facebook cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4763197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:58:13  来源:igfitidea点击:

Where to find the Facebook cookie?

phpjavascriptfacebooklogin

提问by hogsolo

I'm kinda new to this so bear with me. I've installed a "Login with Facebook" button via the Javascript SDK, which works fine. I've also set up the PHP code (facebook.php) I can login successfully to Facebook via javascript but then can't access the UID from the session via php. So, how can I get php to access the session javascript sets? thanks

我对此有点陌生,所以请耐心等待。我已经通过 Javascript SDK 安装了“使用 Facebook 登录”按钮,效果很好。我还设置了 PHP 代码 (facebook.php) 我可以通过 javascript 成功登录到 Facebook,但无法通过 php 从会话中访问 UID。那么,如何让 php 访问会话 javascript 集?谢谢

Edit:(Posting code from below here so it'll be easier to read)

编辑:(从下面发布代码,以便更容易阅读)

I can console log the response and see it.

我可以控制台记录响应并查看它。

FB.getLoginStatus(function(response) {
    console.log(response.session.uid);
});

Refresh the page, and try to access the session that was set via php, it comes up null.

刷新页面,尝试访问通过php设置的会话,结果为空。

require_once(RELATIVE_PATH.'applibs/facebook.php');
$config = array('appId'=>Config::$fb_app_id, 'secret'=>Config::$fb_secret, 'cookie'=>true);
$fb = new Facebook($config);
$fb->getUser(); // this will equal null because the $fb object isnt finding the cookie supposedly set by javascript

*Did some debugging in the facebook.php

*在 facebook.php 中做了一些调试

on line 358... $cookieName = $this->getSessionCookieName();
// retuns fbs_myNumericAppId, seems like that worked..
if (isset($_COOKIE[$cookieName])) {
// FAILS! there is no $_COOKIE['fbs_myNumericAppId']

第 358 行... $cookieName = $this->getSessionCookieName();
// 重新调整 fbs_myNumericAppId,似乎有效..
if (isset($_COOKIE[$cookieName])) {
// 失败!没有 $_COOKIE['fbs_myNumericAppId']

That seems logical as how could logging into facebook.com know what my app ID is?
I also noticed after logging into facebook.com, there's NOTHING facebook related in $_COOKIE. So, in order to log into facebook via facebook.com, then have a website notice that login, you have to run FB.login on a page load. Then you have to set a cookie via the javacript info returned into $_COOKIE so the php can pick it up anywhere else.

这似乎合乎逻辑,因为登录 facebook.com 怎么知道我的应用程序 ID 是什么?
我还注意到在登录 facebook.com 后,$_COOKIE 中没有与 facebook 相关的内容。因此,为了通过 facebook.com 登录 facebook,然后让网站通知登录,您必须在页面加载时运行 FB.login。然后您必须通过返回到 $_COOKIE 的 javacript 信息设置一个 cookie,以便 php 可以在其他任何地方获取它。

回答by David

I happen to have a blog postand some previousanswersthat may be of use to you as reference points.

我碰巧有一篇博文和一些以前的答案,可以作为参考点对您有用。

Have you registered your site on Facebook as an "application"? If not, you'll need to do that. The cookie which Facebook sets that allows your site to access a user's information (once they've agreed to give your "application" access) uses a combination of your application's ID and "secret" (a long alphanumeric string that you must not share, or other applications would be able to impersonate yours) to set an encrypted value with the user's ID and an authentication token unique to the user and your application (representing the permission they've given you to their data).

您是否在 Facebook 上将您的网站注册为“应用程序”?如果没有,你需要这样做。Facebook 设置的 cookie 允许您的网站访问用户的信息(一旦他们同意授予您的“应用程序”访问权限)使用您的应用程序 ID 和“秘密”(一个您不得共享的​​长字母数字字符串,或其他应用程序将能够冒充您的)以使用用户 ID 和用户和您的应用程序唯一的身份验证令牌设置加密值(代表他们授予您对其数据的权限)。

The cookie is posted to your site alongside requests like any other cookie. You'd use your application's secret to decrypt it and extract the user's ID and authentication token, which you would then use to access data via Facebook's graph API.

该 cookie 与任何其他 cookie 一样与请求一起发布到您的站点。您将使用应用程序的机密对其进行解密并提取用户的 ID 和身份验证令牌,然后您将使用它们通过 Facebook 的图形 API 访问数据。