FB.Event.subscribe auth.statusChange not firing in Angular JS - javascript

In my service:
window.fbAsyncInit = function() {
FB.init({
appId : "",
xfbml : false
version : 'v2.0',
status : true
});
};
In My controller:
FB.Event.subscribe('auth.statusChange', function (response) {
console.log(response);
if (response.status === 'connected') {
//Continue
}else{
console.log("FB not connected")
}
});
FB.getLoginStatus(function (response) {
console.log(response);
if (response.status === 'connected') {
//Continue
} else {
// the user isn't logged in to Facebook.
console.log("not logged in");
}
})
I do not see any of console logs messages. infact the auth.statuschange is not getting fired. Please guide me in the correct direction.
TIA

I'd rather put this as a comment, but I can't because of reputation.
Quick question: Are you already logged in on facebook? Because then you would log nothing.

Related

FB.Api not fetch anything

It's the first time I create a Login With Facebook, and I don't know why I can't get any data. Everything goes fine until the dialog to obtain permissions appears; When the user click on "Accept" button I get this error:
GET https://www.facebook.com/impression.php/f3962c3d68/?api_key='API_KEY'….oauth%22%2C%22display%22%3A%22popup%22%7D%2C%22source%22%3A%22jssdk%22%7D
Here's how I have implemented the Login:
function facebookLogin() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
getProfileData();
} else if (response.status === 'not_authorized') {
//app not_authorized
FB.login(function(response) {
if (response && response.status === 'connected') {
getProfileData();
}
},{scope:'email,user_photos'});
} else {
// not_logged_in to Facebook
FB.login(function(response) {
if (response && response.status === 'connected') {
getProfileData();
}
},{scope:'email,user_photos'});
}
});
}
function getProfileData(){
FB.api("/me", function(response){
if(response && !response.error)
{
var name=response.name;
var email=response.email;
}
else
{
console.log(response.error);
}
});
}
API Initialization I think it's fine, I don't get errors about that on the console. Hope you can help me with this. Thanks in advance :D.
UPDATE:
Rarely, printing on the console (I haven't tried it, it was a random thought xD), it fetches the data, but the error stills.
I just found that funny function called "pingFacebook" on the "fb" module from npm.
It's not the same library as you are using, but it's also facebook related.
it basically posts you appId to a page on facebook, which i can't understand why is necessary.
https://github.com/Thuzi/facebook-node-sdk/issues/85
I created an issue for that in hope to get an answer.

Facebook JS api: Not receiving user email despite asking for permissions

For my facebook canvas app I've been trying to access the users email. Even though I ask for it, it still doesn't show up (birthday, which is also an additional permission, does show up). (in the addscope function)
<html>
<head></head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '--------------', // App ID
channelUrl : 'http://www.---------.com/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
});
FB.Event.subscribe('auth.statusChange', function(response) {
if(response.status === 'connected')
{
console.log('Hello there!');
addScope();
}
else if(response.status === 'not_authorized')
{
console.log('not authorized');
addScope();
}
else
{
console.log('not logged in');
addScope();
}
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// ...
} else if (response.status === 'not_authorized') {
// ...
} else {
//...
}
});
};
// 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));
function addScope()
{
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('In addscope function, ' + response.email + response.birthday);
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'email,user_birthday'});
}
</script>
</body>
</html>
An effective way to see if the right permissions are being set through the code is to use the following code (change the fields that you want to check for in the query part):
FB.api({ method: 'fql.query', query: 'SELECT user_status,friends_status,user_photos,friends_photos,user_location,friends_location FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted');
else
console.log(key+' is not granted');
}
});
It is possible the user might not have given the permissions to access email id.
As far as I can see, and providing there are no errors being thrown elsewhere and that the "addScope" method is being called, your code is correct. Just make sure that there actually is a value to return (that the user has a birthday and email, otherwise you will not get it back. as stated here: https://developers.facebook.com/docs/reference/api/user/ )
I am using the FB api and have had no problem getting email and other fields while using the User access token.
remove this :
FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// ...
} else if (response.status === 'not_authorized') {
// ...
} else {
//...
}
});
};
and try again

How to authorize facebook app with javascript

I created this simple code:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('connected');// connected
} else if (response.status === 'not_authorized') {
alert('not_authorized');// not_authorized
} else {
alert('not_logged_in');// not_logged_in
login();
}
And here is the login function, but if I use the login() in the not_autorhized section, the window appear and immediately close (automaticaly)
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
});
}
But how to authorize the app in javascript?
here is my full code: http://pastebin.com/Qv9ayb9V
Updated based on your updated information, now it is a shot in the dark...
You have probably already authenticated your app? and are logged in to facebook.
Try going to facebook.com and logging out. (or using FB.logout()) THen log back in to your site. You should be required to login in the popup
Facebook provides step by step guide on their site explaining how to do this.
https://developers.facebook.com/docs/howtos/login/getting-started/
Did you define the login function? It is not provided by facebook:
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
});
}
FB.login takes an additional param with some config options: one is the scope
{scope: 'email,publish_actions'}
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
}, {scope: 'email,publish_actions'});
}

Facebook app with error

I'm not a Facebook App expert. I grabbed this code from the FB website and I modified it to make it work with my app id (it is the only thing I changed. I added a couple of alerts too). Then I tried to use this code from a local webpage (that is: the page is on my desktop but my laptop is connected to the Internet).
Anytime I run this code I get a browser popup showing this error:
"An error occurred with MyAppName. Please try again later."
Can someone tell me what's going on here? It seems the FB.getLoginStatus never gets called too.
Any help is appreciated.
// initialize the library with the API key
FB.init({ appId: 'myAppID',frictionlessRequests:true });
// fetch the status on load
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
alert("1. not");
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
alert("2. alert");
// the user isn't logged in to Facebook.
}
});
$('#login').bind('click', function() {
FB.login(handleSessionResponse);
});
$('#logout').bind('click', function() {
FB.logout(handleSessionResponse);
});
$('#disconnect').bind('click', function() {
FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
clearDisplay();
});
});
// no user, clear display
function clearDisplay() {
$('#user-info').hide('fast');
}
// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
// if we dont have a session, just hide the user info
alert("entered handleSessionResponse");
if (response.authResponse) {
alert('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
alert('Good to see you, ' + response.name + '.');
});
} else {
alert('User cancelled login or did not fully authorize.');
}
if (!response.session) {
clearDisplay();
return;
}
}
Have you changed the property myAppID to the ID of the Facebook app you are using?
FB.init({ appId: 'myAppID',frictionlessRequests:true });
The myAppID should contain a unique Facebook App ID which you can fetch on the Facebook App page. Maybe you just copied the code without changing this.

Avoid Race Condition in Facebook JavaScript API with FB.logout

I am desperately trying to force a logout before a user starts a new facebook session. But before logout is succesfully called, auth.login seems to login the user and I redirect them. So it seems to be a race condition, but I can't figure out how to get around it.
FB.init({appId: xxxx, status: true, cookie: true, xfbml: true});
//Try to log out user
FB.logout();
//Show the FB div
if(FB) {
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
login(response);
}
});
FB.Event.subscribe('auth.login', function(response) {
login(response);
//login redirects the user. Before logout fires()
});
} // end if(FB).
Something like this maybe:
//initial login check
FB.getLoginStatus(function(response) {
if (response.session) {
//logged in, force logout
FB.logout(function() {
//logged out, subscribe to events
loginEvents();
});
} else {
//not logged in
loginEvents();
}
});
function loginEvents() {
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.Event.subscribe('auth.login', function(response) {
login(response);
//login redirects the user. Before logout fires()
});
}

Categories

Resources