Graph API for fetching facebook information

Posted By : Amit Saini | 10-Sep-2014

Facebook Graph API and its Use .

 

There are various type of ways to use graph API’s of facebook . Here i will describe some of them .
  • If you want to fetch information from a facebook profile than graph API will be :

 

fb.requestWithGraphPath('me', { },'GET', function(e) {});
       
  • if you want to access logo and cover of user profile:  

 

fb.requestWithGraphPath('/me?fields=cover,picture.width(100).height(100)', { }, 'GET', function(e) {});

        

You can even decide the height and width of picture.

  • if you want to access all albums photos from facebook :

 
fb.requestWithGraphPath('/me?fields=albums.fields(photos.fields(id,source))', { }, 'GET', function(e) {});

       
  • if you want to access business pages created over facebook than your graph API will be

 

fb.requestWithGraphPath('me/accounts', { }, 'GET', function(e) {});
       
  • if you want to access facebook business pages albums  photos than :

     
fb.requestWithGraphPath('/' + fbPageID + '?fields=albums.fields(photos.fields(id,source))', { }, 'GET', function(e) {});
     
  • if you want to access facebook business pages logo than :

 

 

fb.requestWithGraphPath('/' + fbPageID + "?fields=picture.width(200).height(200)", { }, 'GET', function(e) {});
       

Graph API Use and proper parsed code :

 

NOTE : parsing can be changed as per functionality.

 

1.

function getPersonalLogoAndCover() {
	fb.requestWithGraphPath('/me?fields=cover,picture.width(100).height(100)', { }, 'GET', function(e) {
		if (e.success) {
			//Ti.API.info("getPersonalLogoAndCove logo data " + JSON.stringify(e));
			//spinner.hide();
			try {
				var data = JSON.parse(e.result);
				//var args = null;
				Ti.API.info("logo data " + JSON.stringify(data));
				// fetching of logo
				if (data.picture) {
					if (data.picture.data) {
						if (data.picture.data.url) {
							argsLogo = {
								logo : data.picture.data.url
							};
						}
					} else {
						argsLogo = {
							logo : false
						};
					}
				} else {
					argsLogo = {
						fbLogoData : true,
						logo : false
					};
				}
				//Alloy.Globals.facebookCallBack(argsLogo);
				//fetching of cover
				if (data.cover) {
					if (data.cover.source) {
						argsCover = {
							cover : data.cover.source
						};
					} else {
						argsCover = {
							cover : false
						};
					}
				} else {
					argsCover = {
						cover : false
					};
				}
				//Alloy.Globals.facebookCallBack(argsCover);
			} catch(e) {
				Ti.API.info("error in getPersonalLogoAndCover function :" + JSON.stringify(e));
			}
		} else if (e.cancelled) {
			Ti.API.info("facebook logo cover data is cancelled by fb server ");
		} else {
			Ti.API.info("facebook logo cover data is failed by fb server ");
		}
	});

}

       

 

2.

  fb.requestWithGraphPath('me', { }, 'GET', function(e) {
		if (e.success) {
			//spinner.hide();
			//value of e after success
			//Ti.API.info("value of e json is after success : " + JSON.stringify(e));
			try {
				var data = JSON.parse(e.result);
				Ti.API.info("facebook data given is : " + JSON.stringify(data));
				Ti.API.info('https://graph.facebook.com/' + fb.uid + '/picture');
				Ti.API.info("AMI : " + fb.accessToken);
				var args = {
					fblogin : true,
					emailID : (data.email) ? data.email : "",
					name : (data.name)? data.name : "",
					address : (data.location) ? data.location.name : "",
					//personal phone no. is not given by facebook grah api
					
					
				};
				//Alloy.Globals.facebookCallBack(args);

			} catch(e) {
				Ti.API.info("try again" + JSON.stringify(e));
			}
		} else if (e.cancelled) {
			Ti.API.info("facebook data fetching is cancelled ");
		} else {
			Ti.API.info("facebook data fetching is failed ");
		}
	});
      

3.  

function getPersonalImagesData() {
	var mainPicturesData = [];
	fb.requestWithGraphPath('/me?fields=albums.fields(photos.fields(id,source))', { }, 'GET', function(e) {
		if (e.success) {
			//Ti.API.info("getPersonalImagesData : " + JSON.stringify(e));
			try {
				var albumsData = JSON.parse(e.result);
				if (albumsData.albums) {
					if (albumsData.albums.data) {
						if (albumsData.albums.data.length > 0) {
							//Ti.API.info("albumsData.data : " + JSON.stringify(albumsData.albums.data));
							var albums = albumsData.albums.data;
							_.each(albums, function(e, index) {
								//Ti.API.info("albumsData : " + index + " " + JSON.stringify(e));
								if (e.photos) {
									if (e.photos.data) {
										var photosData = e.photos.data;
										_.each(photosData, function(e, index) {
											//Ti.API.info("photosData " + index + " " + JSON.stringify(e));											if (e.source) {
												mainPicturesData.push(e.source);
											}
										});
									}
								}
							});
							Ti.API.info(" getPersonalImagesData() ->>> mainPicturesData " + JSON.stringify(mainPicturesData));
						}
					}
				} else {
					Ti.API.info("no personal albums data ");
				}
			} catch(e) {
				Ti.API.info("try again" + JSON.stringify(e));
			}
		} else if (e.cancelled) {
			Ti.API.info("facebook personal data fetching is cancelled ");
		} else {
			Ti.API.info("facebook personal data fetching is failed ");
		}
	});
}
     

Thanks

About Author

Author Image
Amit Saini

Amit is an iPhone and Android application developer with experience in Titanium Framework . Amit likes to play basketball ,volleyball and listens to music in his free time.

Request for Proposal

Name is required

Comment is required

Sending message..