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

Python and Parquet Performance

In Pandas, PyArrow, fastparquet, AWS Data Wrangler, PySpark and Dask. This post outlines how to use all common Python libraries to read and write Parquet format while taking advantage of  columnar storage ,  columnar compression  and  data partitioning . Used together, these three optimizations can dramatically accelerate I/O for your Python applications compared to CSV, JSON, HDF or other row-based formats. Parquet makes applications possible that are simply impossible using a text format like JSON or CSV. Introduction I have recently gotten more familiar with how to work with  Parquet  datasets across the six major tools used to read and write from Parquet in the Python ecosystem:  Pandas ,  PyArrow ,  fastparquet ,  AWS Data Wrangler ,  PySpark  and  Dask . My work of late in algorithmic trading involves switching between these tools a lot and as I said I often mix up the APIs. I use Pandas and PyArrow for in-RAM comput...

Kubernetes Configuration Provider to load data from Secrets and Config Maps

Using Kubernetes Configuration Provider to load data from Secrets and Config Maps When running Apache Kafka on Kubernetes, you will sooner or later probably need to use Config Maps or Secrets. Either to store something in them, or load them into your Kafka configuration. That is true regardless of whether you use Strimzi to manage your Apache Kafka cluster or something else. Kubernetes has its own way of using Secrets and Config Maps from Pods. But they might not be always sufficient. That is why in Strimzi, we created Kubernetes Configuration Provider for Apache Kafka which we will introduce in this blog post. Usually, when you need to use data from a Config Map or Secret in your Pod, you will either mount it as volume or map it to an environment variable. Both methods are configured in the spec section or the Pod resource or in the spec.template.spec section when using higher level resources such as Deployments or StatefulSets. When mounted as a volume, the contents of the Secr...

Andriod Bug

A bug that steals cash by racking up charges from sending premium rate text messages has been found in Google Play.  Security researchers have identified 32 apps on Google Play that harbour the bug called BadNews. A security firm Lookout, which uncovered BadNews, said that the malicious program lays dormant on handsets for weeks to escape detection.  The malware targeted Android owners in Russia, Ukraine, Belarus and other countries in eastern Europe. 32 apps were available through four separate developer accounts on Google Play. Google has now suspended those accounts and it has pulled all the affected apps from Google Play, it added. Half of the 32 apps seeded with BadNews are Russian and the version of AlphaSMS it installed is tuned to use premium rate numbers in Russia, Ukraine, Belarus, Armenia and Kazakhstan.