Is FQL giving wrong typage? - facebook

I am starting with FQL, and I am having some issues with columns' type being not as expected.
For example, docs of table friend says that uid2 is an int, but I receive strings when I lookup this table:
SELECT uid2 FROM friend WHERE uid1 = me()
that gives me:
{
"data": [
{
"uid2": "100004469923697"
}
]
}
and with photo table too, when I expect a string at owner:
SELECT owner FROM photo WHERE owner=me()
...I receive an int:
{
"data": [
{
"owner": 100004536330032
}
]
}
Is that right?
Edit: Not parsed data, raw, as received from source and explorer

It appears that json is loosely typed, and depending on the parser used to generate the json objects numbers can be arbitrarily typed as strings or integers.
According to this Wikipedia entry, "One disadvantage is that the number 25 is reflected as a number, but may have been a string as base type in a database." No reference is given, and a quick Google didn't shed any light on this.
You'll just have to be aware that when getting json data from Facebook, the type received may not always be the type expected.

Related

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!

FQL query doesn't return the correct results

I'm running the following fql query in the graph explorer:
select album_object_id, object_id, aid, pid from photo where object_id=576544082370719
and i get the correct results i want:
{
"data": [
{
"album_object_id": 520085011349960,
"object_id": 576544082370719,
"aid": "396713620353767_120902",
"pid": "396713620353767_2121634"
}
]
}
however, when adding album_object_id to the statement, nothing is returned:
select album_object_id, object_id, aid, pid from photo where object_id=576544082370719 and album_object_id=520085011349960
{
"data": [
]
}
anyone has any idea?
I tried to report it but they said they can't reproduce
https://developers.facebook.com/bugs/448684285215430
here is another example which doesnt work but it supposed to, what is going on?
select object_id, modified from photo where object_id=507652032628572
this gives the correct result, however:
select object_id, modified from photo where album_object_id=468672926526483 and modified>1365522434
no results on this one.
found out that when adding order by modified desc to the statement, results are fine, thats some kind of work around but well, at least works.

Graph API: Retrieving picture dimensions in page feed

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.

Finding who INVITED you :: Facebook FQL Explorer Bug: "Inviter":null

Similar unanswered question, however this question focusing more on where to proceed to now. I am led to believe that there is a bug with Facebook's FQL because the event_member table never returns a non-null answer for "inviter", [tested large and small events, public & private, and self admined events]. There are no reported "inviter" bugs on Facebook's site, and no references elsewhere on the internet. Is it common for Facebook to have an error in their graph ( I should hope not)? Could it be this is now depreciated (With no mark of it's existence)? Should I wait it out or keep trying? If it is a brand new feature of event_member, should there not be a news release etc?
TRY this example on Facebook's FQL explorer: https://developers.facebook.com/tools/explorer?fql=select%20inviter%2C%20inviter_type%2Cuid%2C%20rsvp_status%20from%20event_member%20where%20eid%20%3D%20321938874572158%3B
TL;DR: This doesn't work:
Select inviter, inviter_type,uid, rsvp_status from event_member where eid = 321938874572158;
The graph returned for the inviter field only reports the ONE person who FIRST invited YOU, not anyone else. If you created the event yourself, or "attended" the event without an invite (such as by finding the link), then you wont have an "inviter". So to answer your question, there is not a "crippling bug".
For the part of actual graph results returned shown below:
I have been invited 3 times,though only one ID shows up.
It has a privacy level of "friends of friends".
I am attending.
The command used was SELECT uid, rsvp_status, inviter FROM event_member WHERE
eid=5612079XXXXXXXX;
{
"uid": 51454XXXX,
"rsvp_status": "attending",
"inviter": null
},
{
"uid": 3124XXXX,
"rsvp_status": "attending",
"inviter": 16498XXXX <------ HERE IS MY INVITE, FROM ANOTHER USER
},
{
"uid": 127868XXXX,
"rsvp_status": "attending",
"inviter": null
},
{
"uid": 164982XXXX,
"rsvp_status": "attending",
"inviter": null
}
This is what I can see after testing with my own events and events created by others:
The event_member table's inviter field will return null for the user who is the event creator.
For example, a hypothetical event with eid 111 was created by John who has uid 222. Then John invited Jane who has uid 333.
When we query:
select uid, inviter from event_member where eid=111
we receive:
{
"data": [
{
//Jane's data
"uid":333,
"inviter": 222
},
{
//John's data
"uid": 222,
"inviter": null
}
]
}
So, all users except the event creator has a non-null inviter value.
I don't know if this all tallies with your findings or if what you've found is a different issue/bug.

How to get a Facebook user's current country?

The Facebook Graph API provides a user's current location in two formats: name and ID. ID can be something like 104022926303756. Name can be something like Palo Alto, California or Beijing, China. From these two fields, how do I extract the country? All American locations are in the form [City], [State] whereas all non-American locations are in the form [City], [Country]. Can I code something less hacky than:
$states = array(
'Alabama'
'Alaska',
'Arizona',
// ...
);
$country = 'USA';
if (!in_array($locationName, $states)) {
preg_match('#, ([a-z]+$)#i', $locationName, $match);
$country = $match[1];
}
How about using this: http://developers.facebook.com/tools/explorer/?method=GET&path=fql%3Fq%3DSELECT%20current_location%20FROM%20user%20WHERE%20uid%3Dme()
?
As you can see, using FQL on user table, the JSON you'll receive is something like:
{
"data": [
{
"current_location": {
"city": "Turin",
"state": "Piemonte",
"country": "Italy",
"zip": "",
"id": 115351801811432,
"name": "Turin, Italy"
}
}
]
}
There you have the field country, much more readable :-)
EDIT: The link is broken because brakets are missing at the end of query, anyway the FQL query is:
SELECT current_location FROM user WHERE uid=me()
"me()" can be any user ID.
If you want more information you could use the latitude and longitude from https://graph.facebook.com/104022926303756 and feed them into a reverse geocoder.
http://maps.googleapis.com/maps/api/geocode/json?latlng=37.4293,-122.138&sensor=false
The Google API gives a lot of information, but it can be quite useful.
The country code may be a better idenfifier than the country name for example.
There's also the possibility to select output language. Adding &lang=sv gives you "Kalifornien" as the name of the state instead of "California".
It comes inside the signed request when they hit your app. It comes even when they still didn't approve your app.