Accessing user’s Facebook checkins via Graph API

Your application might want to use a user’s checkin data in various ways, I outline below how to fetch a user’s facebook checkin information and get the Facebook page for that location.

First of all, you need to have the permissions to access a user’s checkin data. Look at http://developers.facebook.com/tools/explorer for information on this.

Once you have the required permissions and the user is logged in via Facebook connect:


//dojo.hitch to make the object scope available
FB.api("/me/checkins", dojo.hitch(this, function(response){
	if(response.data && response.data.length > 0) {
		dojo.forEach(response.data, function(item) {
            var locationId = item.id; 
            var locationName = item.place.name; //name of the place
            // To get a Facebook link to the location
            var locationLink = this.getLocationLink(locationName, locationId);
            console.log(locationLink); //print location link
        });
	}
}));

/**
* Output like this: http://www.facebook.com/pages/Espetus-Churrascaria-Brazilian-Steakhouse/166393336742282
*/
getLocationLink: function(locationName, locationId) {        
        var baseURL = 'http://wwww.facebook.com/pages/';
        var locationNameArr = locationName.split(' ');
        var locationLink = '';
        for(var i = 0; i < locationNameArr.length; i++) {
            locationLink += locationNameArr[i];
            if(i != locationNameArr.length-1) locationLink += "-";
        }        
        return baseURL + locationLink + '/' + locationId;
    },
    

More information on whats available with the Checkin object is outlined here: http://developers.facebook.com/docs/reference/api/checkin/

The /me/checkins request usually returns the most recent 25 checkins, with a hook to access more of the checkin information if required.

Sorting birthdays received from Facebook’s graph API

While working with Friend lists using Facebook’s graph API, you may want to retrieve your friends and do something based on their birthdays. Here’s some sample code to sort your friends based on upcoming birthdays.

FB.api("/me/friends?fields=id,name,birthday,picture, link", dojo.hitch(this, function(response) {
    var birthdays = response.data; //list of friend objects from facebook
    var currentMonth = new Date().getMonth() + 1;
    var upcoming = [];
    dojo.forEach(birthdays, function(item) {
        if (item.birthday) {
            var bday = item.birthday;
            //if the birthday is after today
            if (currentMonth <= bday.substr(0, 2) * 1 && new Date().getDate() <= new Date(bday).getDate()) {
                upcoming.push(item);
            }
        }
    });

    //set the year to current year because of birth years being different.
    var year = new Date().getFullYear();
    upcoming = upcoming.sort(function(a, b) {
        return new Date(a.birthday).setYear(year) - new Date(b.birthday).setYear(year);
    });

    console.log(upcoming);//console log here, but do whatever you want with the sorted friends
}));