Facebook Graph API - get a photos album ID - facebook

I'm writing an application that uploads a photo to facebook. I'm not supplying an album ID so an album is created on behalf of my app.
My question is: how do I get the album ID the photo was uploaded to?
When I GET the the photo information the album isn't present in the JSON which was my hope.

Jimmy Sawczuk's answer is close, but doesn't work because FQL Photo table columns aid and pid are Rest/FQL API ids, not Graph API ids.
Fortunately the object_id and album_object_id columns in this table are indexable (contrary to the documentation) so this should give you both Rest/FQL & Graph ids for the album:
select aid, album_object_id from photo where object_id = ${GRAPH_PHOTO_ID}
In general object_id fields in Rest/FQL API responses are Graph API id (except for photo notifications, where it's the Rest/FQL id).
Re HonkyHonk's answer: not all photo link urls have a set parameter; in this case I think you can find the Rest/FQL API photo id from its other parameters, and then use Rest/FQL API to get the album id:
For users with 32-bit user-ids: calculate (id << 32) | pid
eg for http://www.facebook.com/photo.php?pid=7518353&id=549683637 this gives 2360873244068853905
Otherwise concatenate id, _ and pid
eg for http://www.facebook.com/photo.php?pid=155052&id=100002937903251 this gives 100002937903251_155052
It would help a lot if Facebook could document something about the two types of ids. Also the Graph API Photo schema should include the album's Graph API id.

This is the easy way to get it :

This issue has been bugging me for a while as well, but I think I have cracked the nut.
The album ID is hidden in the link value, so if you have a photo-post object from the Graph API, you can retrieve the Album ID like this:
NSString *link = [item valueForKey:#"link"];
NSString *album_id = nil;
NSRange fRange = [link rangeOfString:#"set=a."];
if(fRange.location != NSNotFound) {
NSInteger firstPos = fRange.location + 6;
NSInteger endPos = [link rangeOfString:#"." options:NSLiteralSearch range:NSMakeRange(firstPos, 25)].location;
album_id = [[link substringWithRange:NSMakeRange(firstPos, endPos - firstPos)] copy];
}
It could probably all be stripped down to one line of code, but I think this is more readable.
For getting the album information use:
[facebook requestWithGraphPath:album_id andDelegate:delegate];
For getting all the photos in an album use:
[facebook requestWithGraphPath:[NSString stringWithFormat:#"%#/photos", album_id] andDelegate:delegate];
Hope this can help someone.

If I'm understanding you correctly, you have the photo ID but need the album ID. You could try using the photo FQL table with the following query:
SELECT aid, pid FROM photo WHERE pid = '<your photo id>'

Long, clunky, but theoretically valid way:
http://graph.facebook.com/userid/albumswill get you a list of all the albums the user lets you see, then you can search the album objects (api here) for created_times that match the time you created the album, which you can store when you create the album.
This is almost certainly a horrible way to do it, but it should work.

Richard Barnett's solution works well for me, I use this as an FQL query through the Graph API to get a JSON response like I would for any other Graph API query:
https://graph.facebook.com/fql?q=select+album_object_id+FROM+photo+WHERE+object_id='[PHOTOID]'
This returns:
{
"data": [
{
"album_object_id": "10150531092908837"
}
]
}
Then you can access the album ID using:
<?php
$strPhotoAlbumID = $objFacebookAPI->data[0]->album_object_id;
?>
This is the cleanest solution i've found as Facebook constantly change the format of their photo and album URLs so parsing these with RegEx wont work as a long term fix.

I ended up just using the legacy REST api since the album ID is passed with the link to the image in the server response. I then used a regex to pull the id out.
Not the best solution and I wouldn't recommend it but I feel its better than adding another several server requests to the code.

function get_album_id_by_album_name($album_name){//return array of data
$fql = 'SELECT aid, owner, name, object_id FROM album WHERE owner=me() and name="'.$album_name.'"';
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $this->facebook->api($param);
return $fqlResult;
}

String albumID = "https://www.facebook.com/photo.php?fbid=192570127478130&set=a.135556629846147.24342.100001754323389&type=1";
albumID = albumID.split("&set=a.")[1];
albumID = albumID.split("\\.")[0];
System.out.println(albumID);

Related

Get Post Id from Url using FQL

I have this Url: http://herbalista.co/1r previously posted on my timeline with the Id = 1635826883323920
How to get that Id with FQL?
I have tryied this
https://graph.facebook.com/fql?q=SELECT+id+FROM+object_url+WHERE+url=%27http://herbalista.co/1r%27
and its return a different id = 1128761283806609
the post Id runs ok to embed a post but the returned Id doesn't works...
I'm using:
$posturl = "https://www.facebook.com/herbalistas/posts/".$id;
<div class="fb-post" data-href="$posturl" data-width="400"><div class="fb-xfbml-parse-ignore"><blockquote cite="$shortened"><p></p></blockquote></div></div>
I think that the returned Id is global for the link but not the original Id from the original post on my timeline
I find the solution:
https://graph.facebook.com/fql?q=SELECT+link_id+FROM+link+WHERE+owner=[USER_ID]+AND+url=%27[URL]%27LIMIT%201000&access_token=[FULL-USER-ACCESS-TOKEN]
it fetch all posts on user timeline and filter it by url

Facebook Graph API - Batch Request for large profile pictures

Trying to grab all of the users Facebook friends information which I can successfully do via a batch request:
NSString *jsonRequest1 = #"{ \"method\": \"GET\", \"relative_url\": \"me/friends?fields=name,picture\" }";
NSString *jsonRequestsArray = [NSString stringWithFormat:#"[%#]", jsonRequest1];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:jsonRequestsArray forKey:#"batch"];
[facebook requestWithGraphPath:#"me" andParams:params andHttpMethod:#"POST" andDelegate:self];
The profile pictures are small in size (50x50) and i need to specify the size of the photos. I've been looking at the graph API docs and i'm struggling to tie the two together - here's the related link: http://developers.facebook.com/docs/reference/api/user/
It says to add the following parameters to the request but like i said, i'm not sure how to tie the two together:
picture?type=large
I'm sure it would work for a single request for a particular user but i'm having no luck through a batch request.
Thanks for your help, it's probably staring me in the face!
The FQL has some advantage like Facebook stated
FQL, the Facebook Query Language, allows you to use a SQL-style
interface to query data exposed by the Graph API. It provides advanced
features not available in the Graph API, including batching multiple
queries into a single call.
Take a look at this tutorial from Facebook.
https://developers.facebook.com/docs/howtos/run-fql-queries-ios-sdk/
But I suggest to use Graph API with Field Expansion, this is very helpful and easier to understand than FQL. Note that Graph API doesn't support for all subfields.
https://developers.facebook.com/docs/reference/api/field_expansion/
Good luck man!
I don't think there is an easy way to do this using the Graph API. It is easy with this FQL query (which can be passed via a batch request):
SELECT uid, name, pic_big FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
If you're looking for a specifically sized picture, take a look at the new profile_pic table.
This is possible by adding a height and width parameter to the URL. FB will attempt to provide you an image that matches your width and height criteria. Your batch would look something like this:
var batch= {
batch: []
};
batch.batch.push({
method: 'GET',
relative_url: fbid + '/picture?height=400&width=400'
});
FB.api('/', 'POST', batch, function (response) {
});
Something like that works for me:
{
"friends": "SELECT uid, name, pic_big FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())",
"profiles": "SELECT id, url, real_width, real_height FROM profile_pic WHERE id IN (SELECT uid FROM #friends) AND width = 300"
}
It's not perfect (two queries to scan) but it' one request.
Based on profile_pic table documentation.
Can be tested in FQL Explorer.

Is there a way to know if a user has liked an object on facebook?

Is there a way to know if a user has liked an object on facebook, given that the user has given my app permission and I have the correct access tokens ??
it seems to me that it is what are you asking for ...
https://developers.facebook.com/docs/reference/fql/like/
For people who come across this page and want a more specific answer, I use the following FQL query via the js sdk in order to find out if a specific user has liked a specific object. If they have, it will return an array with their id. If they haven't, it will return an empty array:
FB.api({
method: 'fql.query',
query: 'select user_id from like where user_id=' + UserIDVar + ' and object_id=' + ActionIDVar,
return_ssl_resources: 1
}, function(response){
console.log(response);
});

comment.create event gives me an useless comment id because I cannot make a query with it [duplicate]

I am using Facebook comment box plugin:
<fb:comments href="${myPageUrl}" num_posts="20" width="630"></fb:comments>
Every thing is working fine. The problem is that I want to store the comment posted into my database. Is there any way to fetch the text posted on the comment box.
I am using the following js to catch comment-create event.
FB.Event.subscribe('comment.create', function(response) {
alert(response.commentID)
});
I'm getting some commentId from this but I don't know how to fetch the exact comment posted on a particular comment-create event.
Coomie: Actually whenever a comment is posted, I catch the event thru 'comment.create'. I was able to catch the event but I was wondering how to get the comment(text) posted at that particular event. Like event.text or event.comment but there was no direct method found
So, now I am manipulating it with fql. Which is somewhat similar to you example. First retrieving the whole list and then selecting the top one.
My sample code is below:
FB.Event.subscribe('comment.create', function(response) {
FB.api({
method: 'fql.query',
query: "SELECT post_fbid, fromid, object_id, text, time from comment WHERE object_id in (select comments_fbid from link_stat where url ='${PageUrl}') order by time desc limit 1"
},
function(response) {
var feed = response[0];
alert(feed.text)
});
});
So this method is giving me exactly the same result I want.
I don't have the complete answer but this should get you on your way.
You can use the facebook graph api to extract information about an open graph id (an open graph id is FB's way of identifying a person, website, application or URL). Eg. this page:
http://www.inhousegroup.com.au/newsroom/23-best-practice-for-advanced-seo/ (the place that fired me)
uses a comment box. The web page has an open id of 10150441190653416. So when you comment on this page facebook sees your comment as a wall post on that page's "wall".
Using the graph api, you can get some JSON info about the page here:
http:/graph.facebook.com/10150441190653416
And you can get the posts from this address:
http://graph.facebook.com/10150441190653416/posts
But you'll have to get an access token.
Then you just have to import the posts on save and compare your db to the JSON and add records as neccessary.
Good luck!
FB.Event.subscribe('comment.create', function(response) {
var commentQuery = FB.Data.query('SELECT fromid, text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
FB.Data.waitOn([commentQuery], function () {
text = commentQuery.value[0].text;
// Use your preferred way to inform the server to save comment
$.post( 'http://example.com/comment', text )
});
});

Are data acquired from Facebook GRAPH API real-time?

I am creating a Facebook application that will download all the photos in an album. The application is created for my personal use and at the same time, learn the Facebook API and JSON.
I can already retrieve the URL of the photos inside an album by calling this url:
http://graph.facebook.com/[album id]/photos?fields=source
The album that I'm trying to download contains 5400+ photos so I tried increasing the limit by adding the limit parameter:
http://graph.facebook.com/[album id]/photos?fields=source&limit=1000
Here's the problem:
The results being returned are only until 2010-07-30T11:20:11+0000. When I tried to modify the query by using the until parameter like so:
http://graph.facebook.com/[album id]/photos?fields=source,link&limit=1000&until=2010-06-01
the data responded correctly. However, if I changed the date to something like 2010-08-05, the latest photo returned will have a created_date of 2010-07-30T11:20:11+0000.
The last photo returned is photo #5000 out of 5695.
Here's my question:
Is the data acquired from Facebook GRAPH Api real-time (or a Monthly update, 2010-07-30)? Or there's just a limit on the number of photos returned on album (like 5000)?
Thanks!
EDIT
There is a 5000 object limit in Facebook. If you know how to break the limit, go here:
Breaking the 5000 object limit in Facebook API
Thanks!
There is indeed 5000 limit on returned objects. You would need to run multiple FQL queries on photo table ordering and limiting the results (<5000) to get all the data (check photo table page for examples). (doesn't work)
When I look at the photos in the graph query you linked
https://graph.facebook.com/119403264763178/photos
I see paging information at the bottom. So, hacking together a quick test
$request = 'http://graph.facebook.com/119403264763178/photos?fields=source&limit=1000';
$response = json_decode( file_get_contents( $request ), true );
$totalCount = 0;
while ( count( $response['data'] ) )
{
echo 'Fetching ' . urldecode( $response['paging']['next'] ) . '<br>';
$totalCount += count( $response['data'] );
$response = json_decode( file_get_contents( $response['paging']['next'] ), true );
}
echo $totalCount;
It would seem that even following the paging data, you can still only retrieve 5000 records.
I'd suggest hitting up the FB forums or opening a bug ticket.