FB.login() does not popup user login dialog? - javascript

function shareFB(){console.log('reached share fb fn');
$(".inviteshare .sendingmsg").show();
FB.getLoginStatus(function(response) {
if (response.authResponse) {console.log('logged in');
shareOnFacebook();
console.log(response);
} else {console.log('not logged in');
FB.login(function(response) {
console.log(response);
if (response.authResponse) {
shareOnFacebook();
console.log(response);
} else {
$(".inviteshare .sendingmsg").hide();
alert("User cancelled login or did not fully authorize");
}
},{scope:'publish_stream'});
}
},{scope:'publish_stream'});
}
i this is my code.it does not open login dialog .if i already logged in it works. that means it opens the shareonfacebook function.but if not logged in it does not woek (FB.login()) method. how do fix it?

Related

Retrieve user info (i.e. location, email) w/ Facebook Javascript API

I'm using the Facebook Javascript SDK to implement Facebook Login. I'm trying to grab the name, email, location, likes, friends, etc. and redirect the user to on-boarding.
I'm able to get accessToken and userID values;however, I'm trying to grab other user information I specifically put in my scope. I've tried to look at Facebook returns undefined for email and birthday
and other questions but no luck 😞
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
console.log(response); // dump complete info
var accessToken = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ accessToken);
var userID = FB.getAuthResponse()['userID']; //get FB UID
console.log("userID: "+ userID);
//refer to getData() function below
getData();
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'public_profile,email,user_friends,user_likes,user_location'});
}
function getData() {
FB.api('/me', function(response) {
console.log("email: "+response.email);
// you can store this data into your database
console.log('Good to see you, ' + response.name + '.');
//check if the loginStatus works
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
//redirect to start/location.ejs
//window.location = "start/location";
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
});
}
Why is this?
I was able to figure it out. Had to define some fields...now I have to figure out how to get a bigger profile pic from the user
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
console.log(response); // dump complete info
var accessToken = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ accessToken);
var userID = FB.getAuthResponse()['userID']; //get FB UID
console.log("userID: "+ userID);
//refer to getData() function below
getData(accessToken);
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {'scope': 'public_profile,email,user_friends,user_likes,user_location'});
}
function getData(accessToken) {
FB.api('/me', 'get', { access_token: accessToken, fields: 'id,name,gender,email,location,friends,likes,picture' }, function(response) {
console.log(response);
});
//check if the loginStatus works
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
//redirect to start/location.ejs
//window.location = "start/location";
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}

FB.login response does not include authResponse

I am creating a Mobile application with facebook Login using the offcial phonegap plugin
https://github.com/phonegap/phonegap-facebook-plugin
The problem is when I authenticate the application it return the response without the authentication response.
FB.login(function(response) {
alert(response.authResponse);
alert("login:"+JSON.stringify(response));
if (response.session) {
alert('logged in');
} else {
alert('not logged in');
}
},
{ scope: "email" });
where the response is {"id":"12345677876","first_name":"xxxxx".........
Therefore "response.authResponse" is always undefined.

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