从 Facebook Javascript SDK 会话对象中提取用户 ID
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取用户ID:
To get the userID:
当您从 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.
检查你的回复。我敢打赌它会说类似“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. Anddisplay: '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.
所以我让它按照我想要的方式工作。这可能不是最好的方法,但经过多次挖掘和挫折后,我希望这可以帮助有同样问题的人走上正轨。
JS 源码:
get.user_graph.php 源码:
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:
get.user_graph.php source: