gpt4 book ai didi

javascript - 从用户的 facebook api 检索数据

转载 作者:行者123 更新时间:2023-11-30 10:28:29 25 4
gpt4 key购买 nike

嘿,所以我很难理解如何从登录我网站的用户那里检索数据,我目前正在使用 javascript sdk,但我想弄清楚如何正确地请求用户的数据,然后如何将它发送到我的服务器端...我认为它可能类似于

req.body.id 

对于 facebook 用户 ID,但我不认为是这样......

这是我登录页面上的 javascript 代码。

script(src='//connect.facebook.net/en_US/all.js')
div#fb-root
script.
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId: 'blank', // 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
});



};



(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));


function authUser() {
FB.login(checkLoginStatus, {scope:'email, user_likes'});

function checkLoginStatus(response) {
if(response && response.status == 'connected') {
alert('User is authorized');
document.getElementById('loginButton').style.display = 'none';
console.log('Access Token: ' + response.authResponse.accessToken);
var uid = response.authoResponse.userID;
var accessToken = response.authoResponse.accessToken;
testAPI();
getFBData ();
} else {
alert('User is not authorized');
document.getElementById('loginButton').style.display = 'block';
}
}


}


function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.' + response.id);
});
};

function getFBData () {
FB.api('/me', function(data){
alert(data.first_name + data.last_name + data.id);
})
};
function fbLogout() {
FB.logout(function (response) {
//window.location.replace("http://stackoverflow.com"); is a redirect
window.location.reload();
});
}




div.centerContent
form(method="POST", action="/login")
fieldset
legend Login
input(type="email", name="email", placeholder="Email", maxlength="20", value= "")
br
input(type="password", name="password", id="password", placeholder="Password", maxlength="20", value= "")
br
input.btn.btn-primary(type="submit", name="login", value="Login")
a(href="/")
button.btn(type="button") Cancel
<fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button>
button#fbLogout(type="button", onclick="fbLogout()") Logout of FB
button#fbLogin(type="button", onclick="authUser()") Login to FB

它是 Jade 质的,但它应该是可读的。

关于如何实际获取用户信息的任何帮助或指导(特别是我正在寻找个人资料图片、访问 token 、用户 ID、名字和姓氏)

谢谢。

编辑:

我在后端使用 node.js 和 mongodb

最佳答案

您的代码有很多问题。我认为最大的一个是你不使用回调中的响应!其他只是变量的拼写错误,例如 authResponse 和 authoResponse。

试试这个:

    function authUser() {
FB.login(checkLoginStatus, {scope:'email, user_likes'});

function checkLoginStatus(response) {
if(!response || response.status !== 'connected') {
alert('User is not authorized');
document.getElementById('loginButton').style.display = 'block';
} else {
alert('User is authorized');
document.getElementById('loginButton').style.display = 'none';
console.log('Access Token: ' + response.authResponse.accessToken);

//This has to be in your callback!
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;

testAPI();
getFBData();
}
}
}


function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.' + response.id);
});
};

function getFBData () {
FB.api('/me', function(data){
alert(data.first_name + data.last_name + data.id);
})
};
function fbLogout() {
FB.logout(function (response) {
//window.location.replace("http://stackoverflow.com"); is a redirect
window.location.reload();
});
}

关于javascript - 从用户的 facebook api 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18385515/

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