Skip to main content

Fetching Facebook Friends using Windows Azure Mobile Services


This tutorial shows you how to fetch Facebook Friends if you have Facebook accessToken.

Here is the the code for Scheduled task called getFriends

function getFriends() {
//Name of the table where accounts are stored
var accountTable = tables.getTable('FacebookAccounts');

//Name of the table where friends are stored
var friendsTable = tables.getTable('Friends');
checkAccounts();
function checkAccounts(){
accountTable
.read({success: function readAccounts(accounts){
if (accounts.length){
for (var i = 0; i < accounts.length; i++){
console.log("Creating query");
//Call createQuery function for all of the accounts that are found
createQuery(accounts[i], getDataFromFacebook);
}
} else {
console.log("Didn't find any account");
prepareAccountTable();
}
}});
}
function prepareAccountTable(){
var myAccount = {
accessToken: "", //enter here you facebook accessToken. You can retrieve it from http://developers.facebook.com/tools/explorer
name: "Ajay Sharma" //enter here your name
};
//Enter new account into table and then run the code from start when done
accountTable.insert(myAccount, {success: function() { checkAccounts(); }});
}
function getDataFromFacebook(url, name){
console.log("Getting data from facebook. " + url);
var request = require('request');
request(url, function friendsLoaded (error, response, body){
if (!error && response.statusCode == 200){
console.log("Retrieved the data from Facebook");
var results = JSON.parse(body).data;
if (results){
console.log("Parsing succeeded");
results.forEach(function visitResult(friend){
var newFriend = {
//Store friends name, id and fetchers name
name: friend.name,
facebookId: friend.id,
fetcher: name
};
friendsTable.insert(newFriend);
console.log("Added new friend " + friend.name);
})
} else {
console.error("Error on parsing");
}
} else {
console.error(error);
}
});
}
function createQuery(account, callback){
friendsTable
.orderByDescending('facebookId')
.where({
//If there's multiple accounts make sure that checking for right friends
fetcher: account.name
})
.read(
{success: function readFriends(friends){
if (friends.length){
//At least some of the friends already on table. Checking only for the new ones
callback(
"https://graph.facebook.com/me/friends?offset=" + friends.length +
"&access_token=" + account.accessToken,
account.name);
} else {
//No friends stored yet so receiving all of them
callback(
"https://graph.facebook.com/me/friends?limit=10&access_token=" + account.accessToken,
account.name);
}
}}
);
}
}

Comments

Popular posts from this blog

How to construct a File System that lives in Shared Memory.

Shared Memory File System Goals 1. MOUNTED IN SHARED MEMORY The result is a very fast, real time file system. We use Shared Memory so that the file system is public and not private. 2. PERSISTS TO DISK When the file system is unmounted, what happens to it? We need to be able to save the file system so that a system reboot does not destroy it. A great way to achieve this is to save the file system to disk. 3. EXTENSIBLE IN PLACE We want to be able to grow the file system in place. 4. SUPPORTS CONCURRENCY We want multiple users to be able to access the file system at the same time. In fact, we want multiple users to be able to access the same file at the same time. With the goals now in mind we can now talk about the major design issues: FAT File System & Design Issues The  FAT File System  has been around for quite some time. Basically it provides a pretty good file structure. But I have two problems with it: 1. FAT IS NOT EXTENSIBLE IN PLAC...

Common Sense Identification of the Security Problems

Organizations make key information security mistakes, which leads to inefficient and ineffective control environment. High profile data breaches and cyber-attacks drive the industry to look for more comprehensive protection measures since many organizations feel that their capability to withstand persistent targeted attacks is minimal. But at the same time, these organizations make some key information security mistakes, that jeopardize their efforts towards control robustness. Although many firms invest in security technologies and people, no one has the confidence that the measures taken are good enough to protect their data from compromises. Below are the 10 worst mistakes which are common to find, and important to address in the path of mature information security posture. If you analyze the cyber security scenarios, and organizational capabilities, the prevailing trend is a vendor-driven approach. In many cases, security professionals adopt the attitude of procuring...

Design of Large-Scale Services on Cloud Services PART 2

Decompose the Application by Workload Applications are typically composed of multiple workloads. Different workloads can, and often do, have different requirements, different levels of criticality to the business, and different levels of financial consideration associated with them. By decomposing an application into workloads, an organization provides itself with valuable flexibility. A workload-centric approach provides better controls over costs, more flexibility in choosing technologies best suited to the workload, workload specific approaches to availability and security, flexibility and agility in adding and deploying new capabilities, etc. Scenarios When thinking about resiliency, it’s sometimes helpful to do so in the context of scenarios. The following are examples of typical scenarios: Scenario 1 – Sports Data Service  A customer provides a data service that provides sports information. The service has two primary workloads. The first provides statistics for th...