我正在尝试使用 Facebook Graph API 来获取有关通过 Facebook 登录的用户的信息。
一开始,我只使用了一个函数,没有出现错误;但随后,我添加了另一个使用 Facebook API 的函数。从那一刻起,这些功能都不起作用。只有当我删除新功能时,旧功能才能再次工作......
我想代码将比我上面描述的更容易理解:
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
user_email = response.email; //get user email
$.ajax({
type: "POST",
url: "http://*******/ajax.php",
data: { action: "login", id: user_id, email: user_email, first_name: response.first_name, last_name: response.last_name }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'publish_actions,email,read_stream,publish_stream'
});
}
除非存在以下功能,否则上述功能将起作用。
function update_autopost(form)
{
FB.api('/me', function(response) {
var autopost_value = form.autopost.checked;
if(autopost_value == false)
{
autopost_value = "off";
}
else{
autopost_value = "on";
}
$.ajax({
type: "POST",
url: "http://*********/ajax.php",
data: { action: "update", id: response.authResponse.userID, autopost: autopost_value }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
}
更详细地说,在添加第二个函数后,会发生两件事:
- 第一个功能停止工作。
- 第二个函数无法从 Facebook Graph API 获取信息(例如, response.authResponse.userID不起作用)。