Detective merry Detective merry - 2 months ago 22
Javascript Question

Facebook login API javascript for mobile apps

I want to intergrate facebook login for my app using Javascript. I have been successful following this youtube guide and I can login through the browser. However, on my android emulator, there was an error and nothing happened when I pressed on the button. On eclipse, this error was returned: "Uncaught reference error: FB is not defined at .../index.html" I am thinking that the probably is caused from the facebook developer side, probably the Valid OAuth redirect URIs or the domain names.

Question:

What should my settings on facebook developer be should I be using if I want to run this function on an app.

Here are my codes:

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<title></title>
<link rel="stylesheet" href="">
<script src="common.js"></script>

</head>

<body>
<script type="text/javascript">
//initialize and setup facebook SDK
window.fbAsyncInit = function() {
FB.init({
appId : '201821216900862',
xfbml : true,
version : 'v2.7'
});

FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
document.getElementById('status').innerHTML = 'We are connected';
// console.log('Logged in.');
}
else if (response.status === 'not_authorized'){
document.getElementById('status').innerHTML = 'We are not logged in';
}
else{
document.getElementById('status').innerHTML= 'facebook not logged in';
}
});
};

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


function login(){
FB.login(function(response){
if (response.status === 'connected') {
document.getElementById('status').innerHTML = 'We are connected';
document.getElementById('login').style.visibility = 'hidden';
// console.log('Logged in.');
}
else if (response.status === 'not_authorized'){
document.getElementById('status').innerHTML = 'We are not logged in';
}
else{
document.getElementById('status').innerHTML= 'facebook not logged in';
}

}, {scope: 'email'});
}

</script>
hi
<div id="status"></div>
<button onclick="login()">Login</button>
</body>


additional info: the reference error only occured in the login() function, I do not have any domain names as I am using localhost,

Answer

Inside the login() function, document.getElementById('login').style.visibility = 'hidden'; is not recognised. Error disappears after the line is removed.