gpt4 book ai didi

javascript - Facebook 注册 - 使用 Javascript 读取数据/签名请求

转载 作者:行者123 更新时间:2023-11-30 06:39:03 24 4
gpt4 key购买 nike

我能够获取使用自定义字段的注册表单,并通过重定向页面上的 cookie 检索自定义字段。我想不通的是如何在下一页检索 Facebook 信息(姓名、电子邮件、生日)(我不是 PHP 专家)。我已经多次浏览文档,“读取数据”部分 ( FB Documentation ),并搜索了互联网,但只能找到 PHP 和 C# 解决方案。

如果有人可以帮助我,我正在寻找 javascript、jQuery 或 ColdFusion 解决方案。

下面是我的代码作为快速引用。就目前而言,第 2 页只是转储了 cookie 信息。

第 1 页

<html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>

<!--- Facebook Call --->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'myAppID', // App ID
channelUrl : 'http://.../channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>

<!--- Output Facebook Box--->
<fb:registration redirect-uri="http://.../page2.cfm"
fields='[
{"name":"name"},
{"name":"email"},
{"name":"displayName","description":"Display Name:", "type":"text"},
{"name":"location"},
{"name":"birthday"}
]'
onvalidate="validateAndSave">
</fb:registration>

<!---Validation + set Cookie --->
<script>
function validateAndSave(form) {
if (!form.displayName) {
return({"displayName":"Display Name is Required"});
}
document.cookie = 'fbInfo='+'displayName='+form.displayName;
return {};
}
</script>
</body>

最佳答案

这是我基于 FB javascript API 反复试验的实现。

function checkFB()
{
if (typeof(FB) === "undefined")
{
var t = setTimeout("checkFB()", 1000);
if (debug) console.log("FB is still undefined, try again in 1 second.");
}
else
{
if (debug) console.log("FB is defined, try to login -> ");

//Function to check the current login status of facebook
FB.getLoginStatus(function(r)
{
if (r.status === "not_authorized")
{
if (debug) console.log("-> NOT AUTHORIZED ");
}
else if (r.status === "connected")
{
if (debug) console.log("-> connected ");

if (r.authResponse)
{
FB.api('/me', function(rr) {
document.getElementById("fbFname").value = rr.first_name;
document.getElementById("fbLname").value = rr.last_name;
document.getElementById("fbEmail").value = rr.email;
document.getElementById("fbID").value = rr.id;
document.getElementById("status").value = "OK";
});

submitFB();
}
else
{
document.getElementById("status").value = "NOT AUTHORIZED";
history.go(-1);
if (debug) console.log("NOT AUTHORIZED");
}
}
else
{
if (debug) console.log("-> not connected -> POP UP BOX expected ");
// start login process
FB.login(function(re)
{
if (re.authResponse)
{
FB.api('/me', function(rr) {
document.getElementById("fbFname").value = rr.first_name;
document.getElementById("fbLname").value = rr.last_name;
document.getElementById("fbEmail").value = rr.email;
document.getElementById("fbID").value = rr.id;
document.getElementById("status").value = "OK";
});
submitFB();
}
else
{
document.getElementById("status").value = "NOT AUTHORIZED";
history.go(-1);
if (debug) console.log("NOT AUTHORIZED");
}
},
{scope: 'email'}); // we define what information we want to get from the user
}
});
}
}

我希望你已经从 facebook 获得了一个 appID 并且有一个 channel 文件,他们需要替换“myAppID”和“http://.../channel.html”。

 FB.init({
appId : 'myAppID', // App ID
channelUrl : 'http://.../channel.html', // Channel File

关于javascript - Facebook 注册 - 使用 Javascript 读取数据/签名请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com