Facebook Api getting total count of friends, but not friends data - javascript

Hi I am using Facebook API javascript SDK, and trying to get logged in user friends data
Here is the scope i am using
FB.login(function(response) {
statusChangeCallback(response);
}, {
scope:'publish_actions,user_friends,public_profile',
return_scopes: true
});
FB.api('/me', function(response) {
$textInput = document.getElementById("searchText");
var request = jQuery.ajax({
type: "POST",
url: "/logged/ajax/facebook",
data: response });
request.done(function( $data ) {
jQuery(".loadingContent").css( "display", "none" );
if($data == "success"){
FB.api('/me/friends', function(data) {
console.log(data);
});
} else {
alert($data);
}
});
request.fail(function( jqXHR, textStatus ) {
});
});
but it returns me
({"data":[],"summary":{"total_count":79}})
data is always empty, though i am getting total count of my facebook friends

Since v2.0, you can only get friends who authorized your App too.
Check out the changelog for more information: https://developers.facebook.com/docs/apps/changelog
This question is also answered in several other threads already:
Get facebook friends with Graph API v.2.0
Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Get ALL User Friends Using Facebook Graph API - Android

Related

Facebook API with Perfex CRM returns only some pages issue

I've bought Perfex CRM and their Facebook leads connector, followed each steps to configure but now I'm facing an issue:
when I Login with Facebook and I select all pages (45) for granting app permission the Facebook API returns only 25 pages (the pages that I created with my profile), but my customer's pages - in which I have admin permission - are not returned.
// Logged into your app and Facebook.
FB.api('me/accounts', function(response) {
console.log('successfully retrieved pages', response);
var pages = response.data;
var token = $('input[name="csrfToken"]').attr('value');
$.ajax({
url: 'facebook_leads_integration/getTable',
type: 'POST',
data: {
pages: pages,
CSRF: token
},
success: function(resp) {
$('#list').html(resp);
//To get data by using leadgenId
},
error: function() {
console.log('something went wrong');
}
});
});
} else if (response.status == 'not_authorized') {
} else {
}
}, {
scope: 'pages_manage_ads,pages_manage_metadata,pages_read_engagement,ads_management,leads_retrieval'
});

Facebook GET /v2.6/{friend-list-id}/members Return Blank in API v2.6

FB.login(function(response) {
console.log('Welcome! Fetching your information.... ');
FB.api(
"/me/friendlists",
function (response) {
if (response && !response.error) {
$.each(response.data, function (key,value) {
//console.log(value.id);
FB.api(
"/"+value.id+"/members",
function (response) {
if (response && !response.error) {
console.log(JSON.stringify(response));
/* handle the result */
}
}
);
});
console.log(JSON.stringify(response));
/* handle the result */
}
}
);
I am Using this code to get friendlist in facebook latest API.
That return blank array in return. I got all my FriendList Id. But after call /members that return blank.
I am testing with "Test application and use 'read_custom_friendlists' permission"
I guess you want to get a list of friends? You can only get friends who authorized your App with the user_friends permission, and this would be the API call: /me/friends.
More information: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Facebook Delete Comment API problems (#200) App does not have sufficient permission for this action for remove comments

I Logged-in with user that I am facebook pages administrator. I write api to delete spam comments in posts by use this method.
$scope.deleteComments = function (commentID) {
if (confirm("Confirm Delete Comments")) {
FB.api(
"/" + commentID,
"DELETE",
function (response) {
console.log(response, commentID);
if (response && !response.error) {
/* handle the result */
}
}
);
}
};
I followed instructions from "https://developers.facebook.com/docs/graph-api/reference/v2.5/comment". Before I use Facebook API I given and Permission with this code.
$scope.triggerLogin = function () {
FB.login(function () {
$scope.checkLoginState();
}, {
scope: "public_profile, publish_pages, manage_pages"
});
};
so that while I use deleteComments() function I get error object from facebook like this
error: Object
code: 200
message: "(#200) App does not have sufficient permission for this action"
type: "OAuthException"
Does anyone know how to fixed this problems, Thank you.
You are most likely not using a Page Token, right now it looks like you are using a User Token. You MUST use a Page Token to delete comments. Generate a Page token by using the /me/accounts endpoint (or /page-id?fields=access_token for a specific Page) and use it in the API call:
FB.api(
'/' + commentID,
'DELETE',
{access_token: 'your-page-token'},
function (response) {
console.log(response, commentID);
if (response && !response.error) {
/* handle the result */
}
}
);
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Post Photo to Facebook Page not associated with User

I am building a website that will allow any authenticated Facebook user to post photos to a specific album associated with a Facebook page. I am using the JavaScript SDK for this task. This is the code I have so far:
window.fbAsyncInit = function () {
FB.init({
appId: "492414284132122",
status: true,
cookie: true,
xfbml: true,
channelUrl: "http://wineoclock.dev.mag/ClockMoments/channel.html"
});
};
function uploadPhoto() {
console.log("called uploadPhoto()...");
FB.login(function (response) {
if (response.authResponse) {
document.getElementById("access_token").value = FB.getAuthResponse()["accessToken'];
var form = document.getElementById("fbPhotoUpload");
var formData = new FormData(form);
// indicate to user that upload is in progress
document.getElementById("btnUpload").value = "Posting...";
document.getElementById("btnUpload").disabled = true;
// post photo to Facebook
$.ajax({
type: "POST",
// post to "Timeline Photos" album
url: "https://graph.facebook.com/283773431737369/photos",
data: formData,
cache: false,
contentType: false,
processData: false,
dataType: "json",
success: function (response, status, jqXHR) {
console.log("Status: " + status);
if (!response || response.error) {
alert("Error occured:" + response.error.message);
}
else {
document.getElementById("fbPhotoUpload").reset();
$("#ThanksModal").reveal();
}
document.getElementById("btnUpload").value = "Post Photo";
document.getElementById("btnUpload").disabled = false;
},
error: function (jqXHR, status, errorThrown) {
console.log("AJAX call error. " + status);
console.log("errorThrown: " + errorThrown);
},
complete: function (jqXHR, status) {
console.log("AJAX call complete. " + status);
}
});
}
else {
alert("Login failed: " + response.error);
}
});
}
Running the code above, I discovered that the selected image gets uploaded, but it is uploaded to Facebook account that is logged in. This is not what I wanted. I wanted to upload it to the target fan page (designated in the AJAX POST request), instead of the logged in user.
I have read on the Internet that I need to request a page access token, instead of a user access token. To do so I would need to log in as the target page, as described in the Facebook doc here.
Is there a way to retrieve the page access token without having to login as the administrator of the Facebook page? If required, can the login be done behind-the-scenes?
EDIT:
I think i misunderstood You; the process described at link i posted results in getting access token for ANY user, you only have to do this once.
I do strongly believe that this is not possible; if it would be doable, practically any app could post at any fanpage.
According to solution from here:Extending Facebook Page Access Token, You need have administrator privileges to convert your short-lived page access token to a long-lived (2 months actually instead of couple of hours). The whole process is well described at the link.

How to store information from Facebook SDK login

I have attempted to implement the Facebook Javascript SDK login into my website but need to store information about the user so that when the user logs back in using Facebook I have information stored about them such as their previous in-website activities. Could I integrate this into a MySQL database?
I think you may searching for the following way of storing logged user information into mysql database,
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
var fbname=response.name;
var fbid=response.id;
$.ajax({
type: "POST",
url: page_for_storing_information,
data: "name="+fbname+"&uid="+fbid,
success: function(msg){
}
});
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});

Categories

Resources