empty friends array in facebook open graph api v2 - facebook

I'm trying to pull a list of my friends in Facebook's OG api v2. If I use the explorer here:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Ffriends&version=v2.0
I can get my friends, but if I program the app and make a json call:
function getUserFriends(){
FB.api('/me/friends', function(response) {
console.log(response);
});
}
The friends array response is an empty object.
I'm using the facebook login button:
<fb:login-button scope="public_profile,email,user_about_me,user_birthday,user_education_history,user_hometown,user_location,user_relationships,user_relationship_details,user_photos,user_friends,read_friendlists" onlogin="checkLoginState();">
</fb:login-button>
and I can access a lot of other data about myself, but not friends. Any idea what I'm missing?

Facebook changed a lot of things recently, for privacy reasons you can only get the friends who are also using the App now. See here:
https://developers.facebook.com/docs/apps/changelog
https://developers.facebook.com/docs/apps/upgrading/
Also see here: Friendslist doesn't work anymore after the powerful access_token

Related

Facebook API. Attend to the event

I would like to make possibility for attending to event in Facebook feed view in my app. Is it possible by POST-request of Graph API?
IF it´s still possible, then by just doing a POST request to the attending endpoint. For example, with the JS SDK:
FB.api('/[event-id]/attending', 'POST', function(response){
console.log(response)
});
Afaik it was possible earlier, and you would need the rsvp_event permission for this.
Edit: I just tried it in the API Explorer, it definitely works: https://developers.facebook.com/tools/explorer/?method=POST&path=[event-id]%2Fattending&version=v2.5 (of course you need to replace the event ID)

Incomplete results retrieving birthdays using Facebook JavaScript API

I'm trying to retrieve all the birthdays of a user friends using Facebook's Javascript API, however I'm getting incomplete results: some birthdays are fetched while others are not. And some of those which are not retrieved have their birthday on their facebook page. I have all the permissions necessary. Am I missing something?
The code I'm using just in case:
FB.api('/me/friends?fields=id,birthday', function(response) { console.log(response) });
Thanks.
Most likely those friends (with missing birthday field) are not allowing apps to access their data.
Also it's a good idea to try the same call using server-side language or the Graph API explorer just to check that the results are consistent.

after user authenticates FB app, how can you get all likes from a specific domain?

I have a website called example.com with several internal pages that are liked.
I created a FB app.
when a user authenticates the FB app, I want to fetch ALL user likes such as: example.com/page1, example.com/page2, etc.
(a single user can have up to 100+ likes on the internal pages of example.com) I essentially want to get all the liked URL's (and associated FB graph ID) the authenticated user has made on my example.com website.
is there a way to do this?
perhaps via a facebook.api call? or perhaps a FQL query? I am open to PHP or JS.
please help! thanks.
...fyi, My initial thought is to for each user, fetch ALL their likes, parse through each one to make sure it came from example.com, and then store those like URL's into a local database. when the user re-visits the site, I will query my database for likes associated with that user. this did not seem the most efficient as it would require storing all likes ever made on my website. any help would be much appreciated.
If you just want to store when a user likes a page on your website why not make use of the edge.create and edge.remove events the Facebook JS SDK provides? Example:
FB.Event.subscribe('edge.create', function(targetUrl, elm) {
// Make a request to server to save 'Like' information
});
FB.Event.subscribe('edge.remove', function(targetUrl, elm) {
// Make a request to server to remove 'Like' information
});
I see in the attributes of the like plugin, that the ref attribute might be what you could use to determine where a like came from. Of course your href attribute would need to be the same on all locations of the like button.
Or, you can register your site with facebook insights and track it there. See https://www.facebook.com/insights/
The User API reference is here: https://developers.facebook.com/docs/reference/api/user/
Here's how you get all the likes via the JS API (unless they're more than one page, they you get a subset of the likes):
FB.api('/me/likes', function(response) {
console.log(response);
});
You need to be logged in, and you need the user_likes permission.

How could I authenticate a user in facebook using oauth?

I want to know how could I authenticate a user, visiting my site, for facebook using oAuth 2.0
You can use the javascript sdk.
Basically go here:
http://developers.facebook.com/docs/reference/javascript/
Add the code to async load the fb all.js
Then authenticating can take many paths... you can do this:
http://developers.facebook.com/docs/reference/javascript/FB.login/
or pass them to here:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL
The you can make proper graph calls to get any necessary info you need with the graph like so:
FB.api('/me', function(response) {
alert(response.name);
});
Further information on that is available here:
http://developers.facebook.com/docs/reference/javascript/FB.api/
Hope this helps.

'Like' a page using Facebook Graph API

Using the Graph API I'd like to be able to have an authorized user "like" a page.
I tried posting the following
https://graph.facebook.com/${PAGE_ID}/likes?access_token=${ACCESS_TOKEN}
And I get an HTTP error 500 accompanied by "Invalid post_id parameter" in the JSON response body. Looks like the /likes resource is suited to liking a wall post and not a page. How do I get this to work with a page?
Facebook has announced support for liking URL's outside of Facebook by using the official built-in Like action. You need to have publish_actions permissions. The graph url for this is:
https://graph.facebook.com/[User FB ID]/og.likes?object=OG_OBJECT_URL&access_token=USER_ACCESS_TOKEN
However, you cannot use this to like a page on Facebook currently, as the documentation states:
For Facebook Pages or websites that do not integrate with Facebook
Authentication, developers should continue to use the Like button
social plugin.
Update June 2016
It's still not possible to like a page using Facebook API, as stated in the /{user_id}/likes documentation page about Creating/Updating/Deleting:
You can't perform this operation on this endpoint.
In previous versions the message was clearer (see the quote below), but the result is the same: it's not possible.
May 2014
The /{user-id}/likes documentation page States about Publishing Likes of Facebook Pages:
You can't publish using this edge, as it is not possible to like a Facebook Page via any API. You should use the Like Button if you want people to be able to like a page in your app.
This is the most obvious and clear statement that has been able to give me an answer to the question.
if your app is an open graph app, now you can like using the api, and no need for the button anymore.
https://developers.facebook.com/docs/opengraph/actions/builtin/likes/
If you want this functionality in a page tab or canvas page within facebook (say to allow for liking the page from within a likegated page), a work around you can involves what Tom Wells suggested in his reply to Luke. You first embed the iframe version of their like button on your page, and then simply listen for the edge.create event in your JS like so:
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
// ...
}
);
In the callback, you can deal with with what happens when the user has liked the page, say like navigating away from the like-gate page, or showing liked-only content.
When the user clicks the iFrame like button, your JS code should receive the edge.create event assuming the iFrame was configured to point to the url of the page in question.
I believe this is not allowed except for specific partner sites, like yelp. The reason is security, you would be able to put some javascript on a page and have everyone that visits that page "Like"ing it without their knowledge.
See How do I "Like" a URL? on the Facebook Platform Developer Forum
You can like an object with its object id using Facebook api using the following piece of code
[FBRequestConnection startForPostWithGraphPath:[NSString stringWithFormat:#"/%#/likes",{object_id}] graphObject:nil completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
//do you customisation post like here
}];