FB.Api not fetch anything - javascript

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.

Related

How to properly use FB.logout/getLoginStatus

I have a webpage where when the user logins in, I execute FB.login:
FB.login(
(response) => {
if (response && response.authResponse) {
console.log(response.authResponse);
} else {
throw new Error('Login failed');
}
},
{ scope: 'permissions,that,I,ask,for' }
);
Running that code, I successfully get back an authResponse with status: "connected". I don't save the tokens in the response in any way.
When the user goes to logout, I call FB.logout:
FB.logout((response) => console.log('Logged out', response));
This gives me the error FB.logout() called without an access token. Now, there are many SO questions (here and here for example) and blog posts that deal with how to handle that error. Specifically, most of them recommend wrapping your logout call in a FB.getLoginStatus call, the idea being that the FB SDK needs to have an internal copy of the access token.
FB.getLoginStatus((response) => {
if (response && response.status === 'connected') {
FB.logout((logoutResponse) => console.log('Logged out, logoutResponse));
}
});
However, when I do that, the response coming back from FB.getLoginStatus has status: "unknown".
How can the status be unknown when 1) I just called FB.login, and then 2), I literally just called FB.getLoginStatus?
What do I need to do to get FB.logout to work properly?
(Tested in a Chrome 66 Incognito tab with no script blockers of any kind running.)
Here is the FB.init call we use:
FB.init({
appId: env.FACEBOOK_APP_ID,
version: 'v2.9',
});
If it's helpful: we wrap all async FB.* API calls with a Promise so that we can chain off of when they finish. For example:
return new Promise((resolve, reject) => {
FB.login((response) => {
if (response && response.authResponse) {
resolve(response);
} else {
reject(new Error('FB.Login failed'));
}
});
});

Issue with Wizcorp phonegap facebook plugin

We are trying to integrate the Wizcorp PhoneGap facebook plugin (https://github.com/Wizcorp/phonegap-facebook-plugin) to support the login process in a new Ionic app.
The login with Facebook seems to work just fine for a new user. The problem is when the users sign off and try to log back in he get the error:
Facebook error: Session was closed and not closed normally
what are we doing wrong here ?
Here's the code that we are using right now:
function fbLogin() {
facebookConnectPlugin.login(['email'], function (response) {
alert("Login Successfull");
alert(JSON.stringify(response));
}, function (error) {
alert("Login ERROR");
alert(JSON.stringify(error));
})
}
function getDetails() {
facebookConnectPlugin.getLoginStatus(function (response) {
if (response.status === 'connected') {
alert("You're connected!");
var userID = response.authResponse.userID;
facebookConnectPlugin.api('/' + response.authResponse.userID + '?fields=id,name,picture.width(400).height(400)', [], function (result) {
alert(JSON.stringify(result));
})
} else if (response.status === 'not_authorized') {
alert("Not Autherized!");
} else {
alert("You're not loggin into Facebook!");
}
});
}
function fbLogout() {
facebookConnectPlugin.logout(function (response) {
alert("Logout success");
alert(JSON.stringify(response));
}, function (error) {
alert("Logout ERROR");
alert(JSON.stringify(error));
})
}
We have checked this link:
https://github.com/Wizcorp/phonegap-facebook-plugin/blob/master/TROUBLESHOOTING.md#no-reply-from-login
however when we try to implement the code below - we get an error
Tyeperror cordova.getActivity is not a function
PackageInfo info = cordova.getActivity().getPackageManager().getPackageInfo("com.goapes.golearn", PackageManager.GET_SIGNATURES);
The above link tells us that we need another Hash Key in our Facebook App Dashboard, so is there another way of obtaining this Hash?
I think you better to try this plugin. Because I developed a app using this plugin and it's in production now. :)
here check this

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

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.

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.

Categories

Resources