How to get Facebook user's friends information using JavaScript SDK - javascript

I am using the Facebook JavaScript SDK. I am able to get the user's information using:
FB.api('/me', function(response) {
....
});
I get the user's friends using:
FB.api('/me/friends', function(response) {
...
}); //.. it only has two values name and id.
I want to get friends Date of birth and location. Is there a FB.api method to get it or can I get it using FB.Data.query?

First your app needs to have the correct permissions, friends_birthday and friends_location in this case.
Next, use the fields parameter in your Graph API request to request the birthday and location fields:
FB.api('/me/friends', {fields: 'name,id,location,birthday'}, function(response) {
//...
});
Depending on privacy settings on some of the friends' accounts, you may or may not be able to access birthday or location data, so make sure you handle the cases where the info is missing. See this question for why.

Doing:
FB.api('/me/friends', function(response) {
...
}); //.. it only has two values name and id.
You will get:
{data: [{id: "xxxxx", name: "Friend name"}, {id: "xxxxx", name: "Friend name"}]}
Than you could access the resource using received id:
FB.api('/xxxxx', function(response) {
...
}); // {first_name: "Name", gender: "male", id: "xxxxxx",.. etc}

You'll need an authenticated user with the user_birthday, friends_birthday, user_location & friends_location permissions. Then you can do something like:
FB.api('/me/friends', { fields: 'name,id,location,birthday' }, function(result) {
// resut has the data
})
Try it out here.

I barely know anything about FB API but have you looked at this:
http://developers.facebook.com/docs/reference/javascript/FB.Data.query
And this:
http://developers.facebook.com/docs/reference/fql/

You can get complete friend's id and name in an array by using FB.api. One needs to initialize the app using FB.init and later checks if user is logged in using FB.getloginstatus(). then only FB.api is useful.
Here is a link which explains how to fetch friends using Javascript SDK.

Related

How can I access user likes information using Graph API?

I am trying to use facebook Graph API to fetch user details.
Below is my FB.api query :
FB.api('/me', 'GET', { access_token: token, fields:'id,name,email,gender,location,likes' }, function(response) {
console.log(response);
alert(response);
});
With above query i am able to fetch id, name and gender but not location, email and likes.
Version for FB SDK : v2.5
I think the issue is related to some permissions required to fetch such private data of user.
Please suggest what need to be done to get all the data.
You need to ask for the permissions in the login process, with the scope parameter. Here is a list of those: https://developers.facebook.com/docs/facebook-login/permissions
HereĀ“s how to use FB.login with additional permissions:
FB.login(function(response) {
if (response.authResponse) {
//user just authorized your app
document.getElementById('loginBtn').style.display = 'none';
getUserData();
}
}, {scope: 'email,user_likes,user_location'});
Source: http://www.devils-heaven.com/facebook-javascript-sdk-login/
Make sure you read about Login Review too: https://developers.facebook.com/docs/facebook-login/review

Listing all the Facebook photos a user is tagged in using response from FB.api

Right now I'm using the Facebook APIs through JavaScript. So far I can log myself in, and now I'm trying to get information about a user who logs into my website. Here is what I have so far:
FB.api('/me/photos', function(response) { console.log(response); } );
However, the response only says: "Object {data: Array[0]}", and the Array is empty.
I've also tried using this code:
FB.api('/me?fields=photos', function(response) { console.log(response); } );
The response I get from the console is: "Object {id: "10152784292783838"}".
How can I get all the photo information? The only ideas I thought of are that I'm not using an access token, but when I tried that it still didn't work. I'm also including 'user_photos' in my scope when I log the user in, but it doesn't change the response. What am I doing wrong?
As long as you're using the app's admin/tester/developer users, you should be able to get the data. If not, the array will be empty, because facebook requires an app review for apps which request more than the basic permissions.
See
https://developers.facebook.com/docs/apps/review/login#do-you-need-review

"Can only send requests to friends or users of this application"

Using the code
FB.ui({
method: 'apprequests',
filters: 'app_non_users',
message: $filter('i18n')('voices-invite-facebookMessage'),
to: users // an array of uIDs
}, function(data){
console.log(JSON.stringify(data));
});
I get the following dialog:
From the documentation I fought I was able to invite friends to use my app, did I understand it wrong?
Thanks
I figured out that actually the people I was inviting weren't "my friends". The access token provided to fetch the friends and the one that the SDK logged in, and used to do the FB.ui request weren't the same. Sorry for your time...

How to retrieve the email id from facebook users

I need to pull the email addresses of all the people who are in my friendlist of facebook.
Is it possible to pull all the email address of my friends using javascript api.
It should be possible to any user, if they provide authentication.
You cannot access the email addresses of a user's friends. You can get their basic public information (user_id, display picture etc.).
To get the email address of these users, they would also have to authorise your application.
you can read the content using by sending a request to the graph api(http://developers.facebook.com/docs/reference/api/) in fb with a valid user access token ....
you can retrieve the user access token via javascript SDK
after getting that request the email of that particular user using fb.uimethods
sample code
FB.api('/', 'POST', {
batch: [
{ method: 'GET', relative_url: 'me'},
...
]
}, function (response) {
console.log(response);
});
the request url shuld be something like "https://graph.facebook.com/me?access_token=user_acess_token" & this will display the basic details about the user ...

FQL Javascript Access Token

I've pretty new to using FQL in any form, sorry if this is a simple question, but I can't seem to get around it.
I have this code that's attempting to get a Friend list:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id);
But the response I keep getting back is
"An active access token must be used to query information about the current user."
When I pull back the response object from the /me query I see all my information such as location, name, work, etc. But I don't see any access token
The code starts like this:
window.fbAsyncInit = function() {
FB.init({appId: '12...0', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
How do I acquire an access token and provide along with the FQL query?
Thanks for any advice ahead of time!
The access token that you have to use in order to do all types of requests to the Graph API (including FQL queries) is given by facebook after the user has succesfuly logged in.
If you are using firebug do a console.log(response) after the user logs in to see the object that you receive.
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
// do something with response
login();
});
It should contain 3 properties: perms, session and status. perms contains a list with permissions granted by the user, the status it's the status of the current user. What matters to you is the session property. This one it's also an object that among others has a access_token property (string) which you can use to do requests to API.
So, your query may look like this:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id + '&access_token='+response.session.access_token);
If you want to get the session in a better way, use FB.getSession, which returns the current session. More info here.
Good luck !

Categories