Graph API: Retrieving picture dimensions in page feed - facebook

I'm using Graph API to get the wall post of a fan page.
I noticed the Facebook iOS app itself is able to determine the exact ratio for its placeholder.
http://developers.facebook.com/tools/explorer?method=GET&path=facebook%2Ffeed%3Ffields%3Dpicture
{
"data": [
{
"picture": "http://photos-b.ak.fbcdn.net/hphotos-ak-frc1/247019_457846347622804_946521095_s.jpg",
"id": "20531316728_10151883063741729",
"created_time": "2013-05-02T16:57:25+0000"
},
:
:
:
{
"picture": "http://photos-e.ak.fbcdn.net/hphotos-ak-ash4/223257_10151498310776729_930531604_s.jpg",
"id": "20531316728_10151498193061729",
"created_time": "2012-10-05T18:42:38+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/20531316728/feed?fields=picture&limit=25&since=1367513845",
"next": "https://graph.facebook.com/20531316728/feed?fields=picture&limit=25&until=1349462557"
}
}
There it contains no information of the pictures' dimension in the API response, which I'd like to have a correct placeholder size with my custom client like the Facebook iOS app.
I tried adding /facebook/feed?fields=picture,width,height but no luck in retrieving the corresponding information.
Is there a possible way to retrieve the pictures's height and width param from the API itself?

No, API doesn't return ALL picture's dimensions based on post feed from /page_id/feed or stream table.
I mentioned all because, what you can do is:
SELECT post_id, created_time, actor_id, attachment, message FROM stream WHERE source_id=PAGE_ID AND created_time<now() LIMIT 50
If the post type is photo:
If the post type is video:
If the post type is link start with fbexternal (extract parameter w and h):
If the post type is link without fbexternal (facebook photo), we stuck here!:
Code Prototype:
if attachment:
image_url = attachment.media[0].src
if image_url:
m = image_url.photo.images
if m:
if m.src == image_url:
image_width = m.width
image_height = m.height
else: #no dimensions info
if host by fbexternal:
#extract parameter w and h from https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBhfbzbNudc_SE8&w=130&h=130&url=http%3A%2F%2F
image_width = 130
image_height = 130
else:
Stuck here, very hard to get extract photo id based on this url, a lot of work!
In conclusion, i highly recommend you to get image width and height after download photo. For example, PHP have getimagesize function and python have Python Imaging Library (PIL).

I'm not sure if there's an easier way of getting the width and height you need but here's a sure but kinda long-winded way.
Taking your example, the ID that you see for the first picture "20531316728_10151883063741729" isn't actually the ID for the picture. It's for the post that the picture is in.
What you can do, is query for that postID
http://developers.facebook.com/tools/explorer?method=GET&path=20531316728_10151883063741729
then it will expose the picture's object_id 457846347622804 (do a search and you'll see what I mean). now you query for that picture's ID and you'll have your width and height served.
http://developers.facebook.com/tools/explorer?method=GET&path=457846347622804
From then on you should be able to get the ratio you need.
If anyone has an elegant way of getting this done, I'd love to know too.

Related

Instagram API response with users not in caption

I want to know what users are tagged in a photo on Instagram, who don't appear in the caption.
Anyone know if this is possible? That is, does the response contain users that are tagged, but that are not physically in the caption text?
https://www.instagram.com/developer/endpoints/tags/
The property you probably want is called users_in_photo. It is an array which contains an array of user objects who've been tagged in the photo. It also contains the geometric coordinates in the photo that the user has been "tagged" at.
Here is an example response using the /media/MEDIA_ID endpoint:
Endpoint Documentation -> https://www.instagram.com/developer/endpoints/media/#get_media
{
"data": {
"users_in_photo": [{
"user": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"position": {
"x": 0.315,
"y": 0.9111
}
}],
// rest of object...
}
}
I think what you are looking for are username mentions in captions and comments, API does not return this list in API, you will have to search for #usernames in the caption string.
You will have to make a separate API call to get the comments in post (u can only get latest 120), and do a manual string search in each of the comments.
users_in_photo in API response will have an array of users that are actually tagged on photo, not username mentions

Graph API get profile image from Comment object

As the Graph API documentation says, the /comment retreives a Comment Object, which contains a from attribute, which represents the user that made the comment. By default, that from attribute comes with an id and a name.
Although I know I can get the profile image with the id, it will depend on the access_token as the way would be like this:
return 'https://graph.facebook.com/' + id + '/picture?type=large&access_token=' + accessToken;
How can I do to get the profile img of the commentator without depending on the access_token? Because when the API retreives a User object, in the fields object, you can request { fields: "id,name,picture }. But how can i do to ask for the picture to the from attribute that comes in the Comment object? As this is not allowed { fields: "id,name,from.picture }
You should be able to ask for second level attributes
https://developers.facebook.com/docs/graph-api/using-graph-api/#fieldexpansion
$ fbapi '/v2.6/me/photos?fields=from{picture}' | jq '.data[0]'
{
"from": {
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-1/xxxx/p50x50/xxxx.jpg",
"id": "9999999999"
},
"id": "000000000"
}

FACEBOOK API: Get likes of particular post on page

I want to get likes on post on the page: "The Jungle Book"
I am able to get the likes of the page using url:
https://graph.facebook.com/" + pageId + "?fields=likes&access_token=" + ACCESS_TOKEN
I have read the documentation
and find that i will get the likes(total count) of the post only returned when the flag summary=true is set
The response should look like this
{
"likes": <somecount>,
"id": "151505938209071_1507814655911519"
}
I am able to get the likes on page but not able to get total number of likes on the particular post on that page.
Can anyone help me regarding how I should set the URL or is there any other way to do it.
From what you described it sounds like it should be like this:
https://graph.facebook.com/" + pageId + "?fields=likes&summary=true&access_token=" + ACCESS_TOKEN
The above is your existing URL with summary=true in between the fields=likes and access_token parameters
However you may be interested in this answer
Quote:
Couldn't find this in the documentation but multiple calls to the API are not necessary. You can use summary when querying a feed or multiple posts. Specify this in the fields parameter.
https://graph.facebook.com/PAGE_ID/feed?fields=comments.limit(1).summary(true),likes.limit(1).summary(true)
This will return a response like this.
{
"data": [
{
....
"summary": {
"total_count": 56
}
...
},
{
....
"summary": {
"total_count": 88
}
...
}
]
}
This will be much faster than making individual requests for each object just to get the number of comments or likes.

How to get photo Like Info from graph explorer?

Can any body tell me how to get the photo Like Info from graph explorer?I tried this fql query
select like_info from photo where object_id=PHOTO ID
Using this query This photo Id-10152460216420695 gives me result
{
"data": [
{
"like_info": {
"can_like": true,
"like_count": 2118,
"user_likes": false
}
}
]
}
But this photo Id-551468638224054 gives me result
{
"data": [
]
}
This two photo are the public and both have more than 1 like.So why the last one can not give me the right result?Waiting for your help...
You can use :
select like_info from photo where object_id="PHOTO ID"
If it fails than it means The photo is invisible for graph api.
One of those photos may not be available via the API due to privacy settings. Even though a photo is public on Facebook itself, there may be other reasons why the piece of content isn't emitted via the API.

Missing Photos from Timeline Photos album

I'm using Facebook Graph API to get all the photos of a user graphPath: me/albums and find something weird with Timeline Photos album.
Photos exist in Timeline album on Facebook, but not returned from API call graphPath/{timeline_photos_album_id}/photos, or they are returned with lower number of photos.
What is the problem?
Logs:
Calling for albums graphPath: me/albums:
.....
{
"can_upload" = 0;
count = 3; !!!!!!!!!!!
"created_time" = "2013-06-18T10:43:27+0000";
description = cool;
from = {
id = 100006100533891;
name = "Mike Mike";
};
id = 1387760311437307;
link = "https://www.facebook.com/album.php?
fbid=1387760311437307&id=100006100533891&aid=1073741829";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2013-06-18T10:47:53+0000”;
},
.....
Calling for album photos graphPath/{timeline_photos_album_id}/photos:
{
data = (
);
}
Unfortunately "it is not a bug, it is by design".
I tried the following (with "user_photos" permission) and it works:
Fetch the album photo count for my user:
https://graph.facebook.com/me/albums?fields=id,count&access_token={access_token}
The result is
{
"data": [
{
"id": "{album_id}",
"count": 161,
"created_time": "2010-11-05T15:41:42+0000"
},
....
}
Then query for the photos of this album:
https://graph.facebook.com/{album_id}/photos?fields=id&limit=500&access_token={access_token}
The result is
{
"data": [
{
"id": "{photo_id1}",
"created_time": "2013-12-29T09:59:52+0000"
},
{
"id": "{photo_id2}",
"created_time": "2013-12-17T10:40:26+0000"
},
...
}
I count the correct number of 161 ids in the result. So everything seems to work fine.
You can also test this via FQL:
https://graph.facebook.com/fql?q=select+pid+from+photo+where+album_object_id+=+{album_id}+limit+1000&access_token={access_token}
there have been changes in the FB API recently, did you try to add the fields you want to get back in the result from the API? In the past, API always returned "standard fields", newest you always have to provide a list of fields of what you want to have back in the result such as ....fields=name,id...
I think you are missing the user_photos permission in the Access Token. The end point:
/{timeline_photos_album_id}/photos is working perfectly fine for me when I tried it using the Graph API Explorer to retrieve photos from my Timeline photos album. And, I don't think that it is necessary to provide the fields.
I'll suggest you should try the same using the Graph API Explorer. Just press the Get Access Token button on the top right and check the user_photos permission to test your request.
In case you are getting lesser results, you can try your query using pagination options limit and offset. This sometimes returns more results as compare to normal(nothing about this on docs, just my personal experience). Something like:
{timeline_photo_album_id}/photos?limit=500&offset=0
But again, as you pointed out, there are strange things going on with the API. Take a look at this article. This article refers to it as an expected behavior of the API!