Javascript FB.api - undefined error - javascript

I am trying to use JS interface for facebook api and it is my first application for it.
Here is a snippet:
HTML:
<div id="fb-root"></div>
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
<script type="text/javascript">facebook_init();</script>
JS:
function facebook_init() {
FB.init({
appId : '<MY APP ID IS HERE>',
channelUrl : '/media/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.api('/me', function(response) {
alert('Your name is ' + response.name);
});
}
channel.html:
<script src="https://connect.facebook.net/ru_RU/all.js"></script>
I have 'Your name is undefined' when I load this page.
But this code
FB.ui({ method: 'apprequests',
message: 'MSG',
title: 'TITLE'});
works as expected.
Could you please help me?
Thanks!

If you log your response variable via console.log(response) you'll see what's wrong:
You'll get an error object with this message:
"An active access token must be used to query information about the current user."
So if you want to get information about the current user you have to send also an Access Token. To get more information about this check the Facebook JS SDK page.

You need to make sure that the user has logged into Facebook and authorized your app, before you call FB.api('/me', ...).
Here's the general information: http://developers.facebook.com/docs/guides/web/#login

To overcome with Undefined problem use the following code:
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '<APP ID>',
status : true,
xfbml : true
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("connected");
connecter=true;
FB.api('/me', function(user) {
alert(user.name);
alert(user.first_name);
alert(user.last_name);
alert(user.email);
});
}
});

Related

Facebook login callback returns name but not email address

I'm using Facebook login in the simplest way, and getting a call back response:
<div id="fb-root"></div>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v6.0&appId=881911462250499&autoLogAppEvents=1"></script>
<script>
window.fbAsyncInit = function() {
FB.getLoginStatus(function(response) {
FB.api('/me', function (response) {
console.log(response);
});
});
}
</script>
I log the response to the console and receive the name and id, but not email address. How can I get the email as well? Upon researching it, it should get it by default, am I wrong?
According to the Fb docs, This is how you do it.
function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus().
console.log('statusChangeCallback');
console.log(response); // The current login status of the person.
if (response.status === 'connected') { // Logged into your webpage and Facebook.
testAPI();
} else { // Not logged into your webpage or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log ' +
'into this webpage.';
}
}
window.fbAsyncInit = function() {
FB.init({
appId : '{app-id}',
cookie : true, // Enable cookies to allow the server to access the session.
xfbml : true, // Parse social plugins on this webpage.
version : '{api-version}' // Use this Graph API version for this call.
});
FB.getLoginStatus(function(response) { // Called after the JS SDK has been initialized.
statusChangeCallback(response); // Returns the login status.
});
};
function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made.
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
You can check login status in this way.
function checkLoginState() { // Called when a person is finished with the Login Button.
FB.getLoginStatus(function(response) { // See the onlogin handler
statusChangeCallback(response);
});
}
By default, fb allows only basic permissions. What you require is additional permission, hence you have to request it this way.
FB.login(function(response) {
// handle the response
}, {scope: 'email,user_likes'});
You can read more about it here. Hope it helps :)
Ok got it, I was confused and it shoudl be here:
FB.api('/me', { locale: 'tr_TR', fields: 'email,name' }, function (response) {

Facebook Share CallBack response Getting Failed``

I am sharing a facebook page on my Timeline and holding a response in a callback. But each time it fails and callback response throws following error:
Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
Here is the HTML Element on which I'm calling the method.
<a onclick="shareonfacebook()"><i class="fa fa-facebook"></i>facebook</a>
And this is the callback method for my facebook button
function shareonfacebook(){
FB.init({
appId : 'APP ID',
xfbml : true,
version : 'v2.8'
});
FB.ui({
method: 'share',
href: 'http://promo.globalhellogroup.com',
}, function(response){
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
});
}
Response is always that error message.

FB api not asking for manage_pages

When logging in as the user that created the app, the manage_pages and publish_stream permissions are asked for and granted. When logging in as another user, the manage_pages permission is not asked for and not granted however publish_stream is. The code is below:
jQuery.getScript('//connect.facebook.net/en_UK/all.js', function(){
FB.init({
appId: ''
});
FB.login(function(response) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/' + pageid + '/feed', 'post',
{
access_token: "",
message : "hello",
to: pageid,
from: pageid,
},
function(response) {
//response
});
}
});
}, {scope: 'manage_pages,publish_stream', return_scopes: true});
});
Any suggestions much appreciated.
You need to submit a request for the 'manage_pages' permission to be granted to your app. Only then can you ask for that permission from other users besides the admin/developer of your app. Check the link below for more info:
https://developers.facebook.com/docs/facebook-login/permissions/v2.2
You must ask facebook to allow your App to request this permission.
Here are the instructions:
https://developers.facebook.com/docs/facebook-login/permissions/#reference-manage_pages

Facebook Login Button callback function

I got a Problem with the Facebook Login on my Website. The Code Below is my FB-Connect Code. The commented part of the code sign's me in to my Website. My Problem is when i "reactivate" this code it logs me in instandly everytime i visit the page. But i want to log the user in when he clicks the "Login with Facebook"-Button. When I clicks the facebook login button a Popup shows up and nothing happens..
Can i somehow call a JS function when i click the FB-Connect button?
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxxxx', // App ID
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
var login = false;
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('connected');
login=true;
var uid = response.authResponse.userID;
//this part of the code below redircts me to the part, where i login to my system.
/*var accessToken = response.authResponse.accessToken;
$.ajax({
type: 'POST',
url: '/?eID=login',
data: $(this).serialize(),
success: function(msg) {
alert("LOGIN");
$('#content').load('/live-session/tickets/');
}
});*/
}});
You need to use FB.login function on click on fb connect button and don't use FB.getLoginStatus
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
From Doc
FB.getLoginStatus allows you to determine if a user is logged in to
Facebook and has authenticated your app.
So, if you don't want to check whether user has logged in on page load, don't call this
When I clicks the facebook login button a Popup shows up and nothing happens..
If you had already authorized the app and authenticated, facebook wont ask you again for login and authorization

FB.api name undefined

I'm working on a new Facebook App for my website using Facebook Connect. I'm using the JS SDK and I try to display the name of the user. Here is my code :
window.fbAsyncInit = function() {
FB.init({
appId : 'myappid', // App ID
oauth : 'true',
channelUrl : '//mychannelfile', // 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.login', function (response) {
// do something with response
});
var connecter=false;
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
connecter=true;
FB.api('/me', function(user) {
console.log(user.name);
});
}
else connecter=false;
});
};
If I check the console the result of user.name is undefined when the user is connected. I don't understand what I'm doing wrong.
Thank you for your help !
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : '176854392480453',
status : true,
xfbml : true
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert("connected");
connecter=true;
FB.api('/me', function(user) {
alert(user.name);
alert(user.first_name);
alert(user.last_name);
alert(user.email);
});
}
});

Categories

Resources