Getting poll results from Facebook Graph - facebook

How can I (if I can) get the results of a poll/question from Facebook graph? Currently I get back something similar to what is below:
"data": [
{
"id": "12345_12345",
"from": {
"name": "My Company Name",
"category": "Category",
"id": "12345"
},
"story": "This is my question",
"icon": "https://s-static.ak.facebook.com/rsrc.php/v1/yy/r/pz5wRf7MB0H.png",
"privacy": {
"description": "Public",
"value": "EVERYONE"
},
"type": "question",
"object_id": "12345",
"application": {
"name": "Questions",
"id": "12345"
},
"created_time": "2012-04-25T12:23:03+0000",
"updated_time": "2012-04-25T12:23:03+0000",
"comments": {
"count": 0
}
}
Can I get more information back about this question? I'm currently using PHP + CURL to get the feed.
Thanks!

From the looks of it, you have queried for the specific post (post-id:12345_12345) : https://graph.facebook.com/12345_12345 .
To get to the question's data we have to query for the question id that is given in this post's data itself:
"type": "question",
"object_id": "12345",
from here we have the question's id, i.e object_id:12345. Using this id we can get the question's info, so query url is : https://graph.facebook.com/12345.
In the question's returned info, we'll also have the options of the poll, it'll be a field named options. Each option's info will be given within this field, and each option has a votes field, which will tell you the number of votes that option received. So you'll have the results of the poll.
Use the graph explorer, to test these things, before you code them. And do read the documentation links to know more about questions.

In general the metadata=1 GET parameter tells you if there is more related data available.

Related

Why do "Sell Something" posts from a private group not contain a message field?

The results I get from the Facebook Graph API (all supported versions) on a private FB group differs from what I see when I go to the actual Facebook website. "Sell Something" posts from the private Group's /feed don't contain information such as the "message" property for some reason, even though I can see it on the website.
From Facebook.com:
Christine : Can't go anymore! Selling last minute
Front Orchestra tickets to the ...
Ingrid : For Sale: Portable booster seat high chair $20 — Pick up
at ...
From the Graph API:
[{
...
"message": "Can't go anymore! Selling last minute Front Orchestra tickets to the ...",
"privacy": {
"value": "",
"description": "",
"friends": "",
"allow": "",
"deny": ""
},
"type": "status",
"created_time": "2015-04-19T00:07:11+0000",
"updated_time": "2015-04-19T04:44:49+0000",
"is_hidden": false,
...
},
{
"privacy": {
"value": "",
"description": "",
"friends": "",
"allow": "",
"deny": ""
},
"type": "status",
"created_time": "2015-04-16T22:11:36+0000",
"updated_time": "2015-04-19T04:41:01+0000",
"is_hidden": false,
...
}]
You will notice that there is only a "message" field in the first post, and not in the second -- although, I don't see any reason that this should be happening.
Add an attachments attribute in fields path parameter. The URL will look like this:
/{group-id}/feed?fields=attachments
Then you'll have an object of type group_sell_product_item in the attachments array inside of each applicable post object. You can also have the post's photos.
I still wonder how to get the price info...

Facebook Graph Api - Retrieve only Wall posts that contain a photo

I need to retrieve wall posts from Facebook for a given account, but I'm only interested in posts that contain a photo. Is there a way to add such a filter to a Facebook graph api query? Something like:
https://graph.facebook.com/DigitasLBiNL?fields=feed&type=photo
The facebook data contains a type property which comes back as photo, so I would think this would be possible, but I haven't had any luck going through the facebook sdk documentation or tinkering around.
I know I could do the filtering myself, but I'd prefer to be able to keep the paging property that facebook returns.
The edges
/{user_id}/home (https://developers.facebook.com/docs/graph-api/reference/v2.1/user/home#read)
/{user_id}/feed (https://developers.facebook.com/docs/graph-api/reference/v2.1/user/feed#read)
should (as per documentation) both be able to take a filter paramenter, containing filter keys from the stream_filter FQL table (https://developers.facebook.com/docs/reference/fql/stream_filter/).
If I run the FQL
select name, value, type, filter_key from stream_filter where uid=me()
I get the following result:
{
"data": [
{
"name": "News Feed",
"value": null,
"type": "newsfeed",
"filter_key": "nf"
},
{
"name": "Status Updates",
"value": 2915120374,
"type": "application",
"filter_key": "app_2915120374"
},
{
"name": "Photos",
"value": 2305272732,
"type": "application",
"filter_key": "app_2305272732"
},
{
"name": "Links",
"value": 2309869772,
"type": "application",
"filter_key": "app_2309869772"
},
{
"name": "Pages",
"value": null,
"type": "public_profiles",
"filter_key": "pp"
},
{
"name": "Video",
"value": 2392950137,
"type": "application",
"filter_key": "app_2392950137"
},
{
"name": "Notes",
"value": 2347471856,
"type": "application",
"filter_key": "app_2347471856"
},
{
"name": "Groups",
"value": 2361831622,
"type": "application",
"filter_key": "app_2361831622"
}
]
}
So, if I choose app_2305272732 as filter key for the photos, I am able to run the follwoing Graph API request successfully:
/me/home?filter=app_2305272732&limit=3
This gives me the three most recent photo posts from my newsfeed.
You want to do this for User/Page feeds (Pages are not supported for filtering according to the docs at https://developers.facebook.com/docs/graph-api/reference/v2.1/page/feed#read), so I tried this:
/me/feed?filter=app_2305272732&limit=3
Unfortunately, this doesn't return only photo posts, at least for me. So I think this is a bug in the Facebook Graph API.
According to
https://developers.facebook.com/docs/graph-api/reference/stream-filter/
you can use the type filter with value photo

How to get realtime like count from a picture posted by my app?

I'm developing an application and what I want it to do is:
Post a poto on the user's wall
Get, in realtime, the number of likes that this post will recieve
The first problem I'm having is to get the likes count based on the post-id, this is my photo upload code:
FB.api('/photos', 'post', {
message: $user_submited_string,
url:'http://example/url/image.jpg'
}, function(response){
if (!response || response.error) {
//error handler
} else {
var posted_content_id = response.postid;
console.log(posted_content_id);
})
But when I put this posted_content_id variable into the Open Graph Debugger I won't get the number of likes in return:
{
"id": "1685033176_4107146412059",
"from": {
"name": "João Paulo Apolinário Passos",
"id": "1685033176"
},
"picture": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-prn1/548929_4107355417284_1018318871_s.jpg",
"link": "https://www.facebook.com/photo.php?fbid=4107355417284&set=a.4107146292056.1073741826.1685033176&type=1&relevant_count=7",
"name": "My Test App Photos",
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/1685033176/posts/4107146412059"
},
{
"name": "Like",
"link": "https://www.facebook.com/1685033176/posts/4107146412059"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "photo",
"status_type": "added_photos",
"object_id": "4107355417284",
"application": {
"name": "My Test App",
"id": "178883808932511"
},
"created_time": "2013-04-10T01:20:12+0000",
"updated_time": "2013-04-10T01:20:12+0000"
}
Which does not contain the like count of my picture. So the first question is: how to get the like count of the image that the user inserted?
The second thing is that, after getting the number of likes, I want to rank them. To do that, I thought about storing them into a database and set a cron job to check new posts and update the rank with new posts. When I was looking how to do that I came across the "Realtime Updates" section of the Facebook Developers Documentation. But for me it's not clear if I can get this like count in real time and how. And that is my second question: Can I get the number of likes of an image in realtime, using native "Realtime Updates"? And if that's not possible, how to create my own counter? Extended token and cronjobs?
I'm asking this two questions in one because maybe the second question's anwser is also the first one. And the last thing I need to know: even if it's doable, doing something like that violates on someway Facebook Policies?
This is because you are querying for the Post Id, not the Photo Id.
When you upload the photo, a post is also automatically published on the feed of the user.
To read the Photo id, you must use the response.id property.
In your example the id of the photo is 4107146412059, you can derive it from the post id that is in the form <user-id>_<photo-id>, but don't count on it, since that can change in the future.
Doc on the Photo object in the Graph API documentation
Real time updates:
You can get real time push notification from Facebook when the User feed is updated:
but this would be overkill, since you'll get all the posts to the User feed, not only to your photo. Sadly, I think the only way is to poll for updates.
Other info on Real time updates: Facebook documentation

How do you retrieve the "Place" field of a Facebook Graph Post object?

I am trying to retrieve the "Place" field of a Facebook Graph Post object from a Facebook friend. After obtaining an access token with the friends_status and read_stream permissions, I request from the Graph API like so:
http://graph.facebook.com/[user_id]_[post_id]
What is returned are some of the fields described in the Graph API docs for Post:
{
"id": "1164358582_2458311174854",
"from": {
"name": "Joe Blo",
"id": "1164358582"
},
"message": "Is pretty bummed today",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/1164358582/posts/2458311174854"
},
{
"name": "Like",
"link": "http://www.facebook.com/1164358582/posts/2458311174854"
}
],
"type": "status",
"application": {
"name": "Facebook for Android",
"id": "350685531721"
},
"created_time": "2011-11-20T03:23:04+0000",
"updated_time": "2011-11-20T12:12:49+0000",
"comments": {
"data": [
{
"id": "1164358582_2458311174852_2962531",
"from": {
"name": "Sue Candy",
"id": "1056617421"
},
"message": "OMG I'm so sorry!!!",
"created_time": "2011-11-20T03:25:06+0000"
}
],
"count": 1
}
}
This is all fine and dandy, except for the fact that when I see this Post within my Facebook stream, it also shows a location accompanying the post:
9 hours ago near El Reno, OK
I expected the El Reno, OK Place object returned as a field within this Post, but I don't see it.
In the Facebook Graph API Explorer, I've tried enabling almost every permission and I am not seeing any difference in the response (no "Place" field returned). Am I going about this incorrectly?
Not 100% sure but I think you can only get the "place" info if it is of type "checkin". I totally get where you are coming from, I wondered about that myself.
https://developers.facebook.com/tools/
Try the Graph API Explorer Tool if you haven't already looked at it.

Facebook Graph API - Get like count on page/group photos

I've been testing graph API and ran into a problem. How can I get like count from photos of a page/group?
I'm administrator/creator of a group. When entering in https://developers.facebook.com/tools/explorer/ certain photo ID from that group it brings almost all data, even comments, but not the like count. For like part it needs (according to docs) access token despite the fact that anyone can access that info.
How to get access token of my page/group with required permissions and how to use it to get info I need?
If possible I would like to get JSON from a single address if it is possible.
This is possible with a page (even without an access token!) and here's how:
Visit the page on the graph
Get the page's id by going to the page's url:
https://www.facebook.com/pages/platform/19292868552
The number on the end of the URL is the page's id. Take that id and use it with the graph explorer (here) or just visit it directly.
Visit the page's albums
Now appending albums to that url will give you all the albums the page has, including wall photos:
https://graph.facebook.com/19292868552/albums
The output looks like this:
{
"data": [
{
"id": "10150160810478553",
"from": {
"name": "Facebook Platform",
"category": "Product/service",
"id": "19292868552"
},
"name": "Bringing Operation Developer Love to Europe",
"description": "Blog post: http://developers.facebook.com/blog/post/479\n\nVideos and presentations uploaded here: http://developers.facebook.com/videos/",
"location": "Berlin, Paris, London",
"link": "https://www.facebook.com/album.php?fbid=10150160810478553&id=19292868552&aid=285301",
"cover_photo": "10150160811078553",
"count": 32,
"type": "normal",
"created_time": "2011-04-06T23:05:44+0000",
"updated_time": "2011-04-06T23:33:20+0000",
"comments": {
..... etc ....
Selecting an album
For each object in the data array there is an id and a name. Using these two fields you can select the album that contains the photos you want. The first album in this result is "Bringing Operation Developer Love to Europe". Lets look at this albums photos.
Seeing Photos
If you've followed the answer up to this point the next step should be fairly obvious. Use the id for the album you want and append photos to the graph url:
https://graph.facebook.com/10150160810478553/photos
Seeing a Photo's likes
Much like selecting an album, simply use an id in the output of the above step and append likes to the url to see a photos likes:
https://graph.facebook.com/10150160813853553/likes
Output:
{
"data": [
{
"id": "1163036945",
"name": "Aditya Pn"
},
{
"id": "1555885347",
"name": "Nadin M\u00f6ller"
},
{
"id": "100001643068103",
"name": "Umut Ayg\u00fcn"
},
{
"id": "100000165334510",
"name": "Alessandra Milfont"
},
{
"id": "100001472353494",
"name": "Sayer Mohammad Naz"
},
{
"id": "1051008973",
"name": "Jenson Daniel Chambi"
},
{
"id": "100000233515895",
"name": "Ruby Atiga"
},
Using this output you can simply count the number of entries in the data array to get the like count.
Note that all of this is possible from using the graph explorer by clicking on ids in the output box and the connections sidebar (except for the last /likes connection, which will hopefully be added soon. I hope this helps. Also, you do not need an access token to do any of this because pages are public. Hope this helps!
You can get the comments/likes count without having to paginate through all likes by using the fields parameter in combination with likes.limit(1).summary(true). For example, a search api query for pizza below will give you all public posts and their likes count summarized:
https://graph.facebook.com/search?q=pizza&type=post&fields=id,name,likes.limit(1).summary(true)
results (truncated):
{
"data": [
{
"id": "47883936273_659693910762305",
"name": "Instagram",
"created_time": "2014-02-16T01:15:29+0000",
"likes": {
"data": [
{
"id": "100002243084532",
"name": "Yvette Martin"
}
],
"paging": {
"cursors": {
"after": "MTAwMDAyMjQzMDg0NTMy",
"before": "MTAwMDAyMjQzMDg0NTMy"
},
"next": "https://graph.facebook.com/47883936273_659693910762305/likes?limit=1&summary=1&after=MTAwMDAyMjQzMDg0NTMy"
},
"summary": {
"total_count": 13682
}
}
},
{
"id": "136336876521150_314001148754721",
"name": "Pizza Box Turns into Plates & Storage Unit!",
"created_time": "2014-02-15T21:20:00+0000",
"likes": {
"data": [
{
"id": "100005373008864",
"name": "Liliana Campoli"
}
],
"paging": {
"cursors": {
"after": "MTAwMDA1MzczMDA4ODY0",
"before": "MTAwMDA1MzczMDA4ODY0"
},
"next": "https://graph.facebook.com/136336876521150_314001148754721/likes?limit=1&summary=1&after=MTAwMDA1MzczMDA4ODY0"
},
"summary": {
"total_count": 2792
}
}
}
/me/feed returns a LIKES field
I used a solution where I simply find the number of items in the array which display LIKES
Object.keys(item.likes.data).length
This returns the "length" of an object -- which is equal to the number of likes.
http://graph.facebook.com/223766074374957
there is a likes property in the response
{
"name": "Bejeweled Blitz",
"is_published": true,
"website": "https://apps.facebook.com/bejeweledblitz/",
"username": "bejeweledblitz",
"products": "Bejeweled Blitz\nBejeweled\nPlants vs. Zombies\nPeggle\nZuma\nChuzzle\nBookworm Adventures\n \nFor a complete list, please visit www.popcap.com\n ",
"about": "We're the award-winning hit, Bejeweled Blitz! The lightning-fast puzzle game where you have just 60-seconds to match as many gems as you can! Start Blitzing now!",
"general_info": "Speed is the name of the game in Bejeweled Blitz! Bejeweled Blitz is a speedier take on the classic Bejeweled match-3 gem game mechanic. Your goal is to make as many matches as you can in 60-seconds to really make your score soar! But you won't be going it alone! There are special ways to enhance your Bejeweled Blitz game - like Boosts (power-ups like Scrambler, which moves all the gems around the board or +5 seconds, to add 5 seconds more gem-matching time!) and the popular, Rare Gems. Rare Gems are like extra-special power-ups that can REALLY take your game to the next level!",
"talking_about_count": 22487,
"category": "App page",
"id": "223766074374957",
"link": "https://www.facebook.com/bejeweledblitz",
"likes": 5796324,
"cover": {
"cover_id": 383412771743619,
"source": "http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash4/s720x720/417018_383412771743619_1056862875_n.jpg",
"offset_y": 0
}
}