从 Facebook Javascript SDK 会话对象中提取用户 ID

发布于 2024-10-22 18:50:06 字数 783 浏览 2 评论 0原文

我正在使用 Facebook 的 Javascript SDK“FB.ui”来启动 OAuth 对话框。单击允许后,我需要捕获会话对象、提取用户 ID 并在我的脚本中进一步使用它。由于某种原因,我无法使其正常工作,即使会话确实存在,我仍然无法定义。

<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
    appId  : '***************',
    status : true,
    cookie : true,
    xfbml  : true
});
FB.getLoginStatus(function(response) {
    if (response.session) {
        //do something
    } else {
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            alert(response.session.uid); //returns underfined
        });
    }
});
</script>

I am using Facebook's Javascript SDK "FB.ui" to pull up an OAuth dialog. Once you click allow, I need to capture the session object, extract the user id and use it further in my script. For some reason I cannot get this working properly, I keep getting undefined, even though the session does exist.

<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
    appId  : '***************',
    status : true,
    cookie : true,
    xfbml  : true
});
FB.getLoginStatus(function(response) {
    if (response.session) {
        //do something
    } else {
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            alert(response.session.uid); //returns underfined
        });
    }
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百变从容 2024-10-29 18:50:06

获取用户ID:

FB.getLoginStatus(function(response) {
  alert(response.authResponse.userID);
});

To get the userID:

FB.getLoginStatus(function(response) {
  alert(response.authResponse.userID);
});
_失温 2024-10-29 18:50:06

当您从 Facebook 的登录按钮登录时,它会在您的系统中存储 cookie,其中包含您的访问令牌,该令牌也是唯一的。该 cookie 还包含您的 Facebook ID。

When you login from login button of facebook then it stores cookies in your system that contains your access token that is also unique. That cookies contains your facebook id also.

定格我的天空 2024-10-29 18:50:06

检查你的回复。我敢打赌它会说类似“display”必须是“popup”、“iframe”或“hidden”之一。 显然,oauth 对话框无法使用默认的“page”显示来调用。 display: 'iframe' 要求您包含 access_token,这就是您首先调用 oauth 对话框的原因 >:l 。

我认为 FB.ui 当前不能用于获取 oauth 对话框,除非您想使用弹出窗口,而现在大多数人都已阻止弹出窗口。

Check your response. I bet it says something like "display" must be one of "popup", "iframe" or "hidden". Apparently the oauth dialog cannot be called with the default "page" display. And display: 'iframe' requires that you include an access_token, which is why you're calling the oauth dialog in the first place >:l .

I don't think FB.ui can currently be used to get the oauth dialog, unless you want to use a popup, which most everyone has blocked nowadays.

江湖彼岸 2024-10-29 18:50:06

所以我让它按照我想要的方式工作。这可能不是最好的方法,但经过多次挖掘和挫折后,我希望这可以帮助有同样问题的人走上正轨。

JS 源码:

FB.getLoginStatus(function(response) {
    if (!response.session) {
        //initiate FB OAuth js popup dialog
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            if (response.session) { //if permission Allowed
                var thesession = response.session;
                var thesession = eval('(' + thesession + ')'); //decode json
                //POSTing to local file get.user_graph.php, to fetch user info
                $.ajax({
                    type: "POST",
                    url: "get.user_graph.php",
                    data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid,
                    dataType: "text",
                    success: function(user_graph){
                        var user_graph1 = eval('('+user_graph+')');
                        alert(user_graph1.name); //users name
                        alert(user_graph1.id); //users id
                        alert(user_graph1.email); //users email
                        alert(user_graph1.link); //users profile link
                    }
                });
            } else {
                //if permission Not Allowed
            }
        });
    }
});

get.user_graph.php 源码:

//exchange our session for an access_token
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions');
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred');
$curl_token = curl_init(POSTURL);
curl_setopt($curl_token, CURLOPT_POST,1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS);
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl_token, CURLOPT_HEADER,0);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1);
$token = curl_exec($curl_token);

$token_decoded = json_decode($token,true);
//get the user graph (personal info)
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']);
echo $user_graph;

So I got this to work as I would like. This may not be the best way, but after much digging around and frustration I hope this could help somebody with the same question get on the right track.

JS source:

FB.getLoginStatus(function(response) {
    if (!response.session) {
        //initiate FB OAuth js popup dialog
        FB.ui({
            method: 'oauth',
            display: 'page',
            scope: 'email',
            perms: 'email'
        },
        function(response) {
            if (response.session) { //if permission Allowed
                var thesession = response.session;
                var thesession = eval('(' + thesession + ')'); //decode json
                //POSTing to local file get.user_graph.php, to fetch user info
                $.ajax({
                    type: "POST",
                    url: "get.user_graph.php",
                    data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid,
                    dataType: "text",
                    success: function(user_graph){
                        var user_graph1 = eval('('+user_graph+')');
                        alert(user_graph1.name); //users name
                        alert(user_graph1.id); //users id
                        alert(user_graph1.email); //users email
                        alert(user_graph1.link); //users profile link
                    }
                });
            } else {
                //if permission Not Allowed
            }
        });
    }
});

get.user_graph.php source:

//exchange our session for an access_token
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions');
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred');
$curl_token = curl_init(POSTURL);
curl_setopt($curl_token, CURLOPT_POST,1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS);
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl_token, CURLOPT_HEADER,0);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1);
$token = curl_exec($curl_token);

$token_decoded = json_decode($token,true);
//get the user graph (personal info)
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']);
echo $user_graph;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文