Link to home
Start Free TrialLog in
Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on

list friends list using the Facebook java SDK

Hello,

I am looking for some basic help i am trying to pull friends names from a user using the facebook javascript Graph API.

here is the function that I have

  function friendslist()
  {
  console.log("Made it to friends list");
  /* make the API call */
		FB.api("/{friend-list-id}",
			function (response) {
			  if (response && !response.error) {
				console.log(response.id);
				 document.getElementById('status').innerHTML = response.id ;
			  }
			}
		);

Open in new window

Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Here you need the user friends and not the friends-list they are not the same so you could get the friend of specific user using :

/{user-id}/friends

Open in new window


Then loop through the data like :

for ( k in r.data ){
    console.log( r.data[k].id );
}

Open in new window


The full script will be :

function friendslist()
{
	FB.api("/{user-id}/friends",
		function (response) {
		  if (response && !response.error) 
		  {
			  for ( k in response.data ){
			    console.log( response.data[k].id, response.data[k].name  );
			 }
		  }
		}
	);
}

Open in new window

Avatar of Chris Jones

ASKER

Hello,

i just tested the code and added a few debug lines and noticed it's only getting to this line and nothing after.

		  if (response && !response.error) 

Open in new window

Ok good job sir, let us now check the response content before the if statement like :

console.log( response );

Open in new window

hmm looking at the error now let me know if you have any insight it may be a permission issue.


(#803) Some of the aliases you requested do not exist: {user-id}

Open in new window

aaah, you should replace the expression {user-id} by the real id of the user you want to get the list of his friends.
getting closer I passed in the user's ID but I think the call is expecting something else

error: {message: "(#803) Some of the aliases you requested do not exist: {10156781396921240}", type: "OAuthException", code: 803, fbtrace_id: "EvfQq6RkO5B"}
__proto__: Object

Open in new window

i even passed in the access token and it doe snot like that value as well
Make sure the access_token isn't expired and share with us the response when you pass it.
here is the response from the code with the access token passed in.

{error: {…}}error: {message: "(#803) Some of the aliases you requested do not ex…ZAuYoUKUx8vZCI1upOMihUF2U5csKwdUOwT0FvFeQyfwZDZD}", type: "OAuthException", code: 803, fbtrace_id: "Cq3sszJeg6D"}__proto__: constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()

Open in new window


update function

function friendslist(UserID, AT)
{
	FB.api("/{"+ AT +"}/friends",
		function (response) {
		console.log(UserID);
		console.log(AT);
		console.log('friends list function start');
		console.log( response );
		  if (response && !response.error) 
		  {
		        console.log('If response in friends function');
			   for ( k in response.data ){
			    console.log( response.data[k].id, response.data[k].name  );
			 }
		  }
		}
	);
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.