Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
benorama edited this page Sep 20, 2011 · 14 revisions

Home | Installation | Migration | Usage | Examples

This page explains basic usage of the components available in the Facebook ColdFusion SDK. To use the components in your application, you must first install the SDK on your server.

Documentation

The HTML documentation of the CFCs is available here : [http://affinitiz.com/facebook-cf-sdk/documentation/]

FacebookApp.cfc

Configuration

You must first create an application on Facebook Developers, in order to get an app ID and a secret key and configure your application.

Activate all the migration settings, especially Canvas session parameter and OAuth 2.0 for Canvas.

Initialization

The examples are a good place to start. The minimal you'll need to have is:

import facebook.sdk.FacebookApp;
facebookApp = new FacebookApp(appId='YOUR APP ID', secretKey='YOUR SECRET KEY');

User session

To get current user session or id (in an apps on Facebook.com or on a Facebook external website):

userId = facebookApp.getUserId(); // return 0 if not logged or if app is not installed

Logged in vs Logged out:

<cfif userId gt 0>
	<a href="#facebookApp.getLogOutUrl()#">Logout</a>
<cfelse>
	<a href="#facebookApp.getLogInUrl()#">Login</a>
</cfif>

FacebookGraphAPI.cfc

Authentication

The Graph API uses OAuth 2.0 for authorization, so you must have an OAuth access token to call Graph API. If you do not provide an access token, you can only see public data.

To get a user OAuth access token for the current logged user, use the FacebookApp component. Security restrictions will apply depending on the extended permissions you defined when the user authentified your application.

userAccessToken = facebookApp.getUserAccessToken();
facebookGraphAPI = new FacebookGraphAPI(accessToken=userAccessToken);

It is also possible to get an OAuth access token for the current application.

applicationAccessToken = facebookApp.getApplicationAccessToken();
facebookGraphAPI = new FacebookGraphAPI(accessToken=applicationAccessToken);

Graph API

You can then access the Graph API based on your authentication security. Most write operations require extended permissions publish_stream for the active user.

// Get my profile info
me = facebookGraphAPI.getObject(id='me');
// Get my profile info with specific fields only and metada
me = facebookGraphAPI.getObject(id='me', fields='id,name', metadata=1);
// Get my feed
feed = facebookGraphAPI.getConnections(id='me', type='feed');
// Get my recent photos
photos = facebookGraphAPI.getConnections(id='me', type='photos', since=dateAdd('m', -2, now()));
// Get my friends
friends = facebookGraphAPI.getConnections(id='me', type='friends');
// Get my friends with paging
friends = facebookGraphAPI.getConnections(id='me', type='friends', limit=10, offset=2);
// Search in my friends
friends = facebookGraphAPI.getConnections(id='me', type='friends', search='emeric');
// Public search
search = facebookGraphAPI.search(text='watermelon', type='event');
// Publish post to my wall
facebookGraphAPI.publishPost(profileId='me', message='I am writing on my wall!');

FacebookRestAPI.cfc

The old REST API is the previous version of the Graph API. It is recommanded to use the Graph API for any new development.

Old Rest API

To get the same information as the Graph API:

facebookRestAPI = new FacebookRestAPI(userAccessToken);
me = facebookRestAPI.call(method='users.getInfo', fields='uid,first_name,middle_name,last_name,name,locale,current_location', uids='#userId#');
friends = facebookRestAPI.call(method='friends.get', uid='#userId#');

Facebook Query Language

For more complex and faster data retrieval, you can also use Facebook Query Language (FQL).

me = facebookRestAPI.executeQuery('SELECT name FROM user WHERE uid = 204686');
// Single query
friends = facebookRestAPI.executeQuery("SELECT flid,name FROM friendlist WHERE owner=204686");
// Single complex query
queryResult = facebookRestAPI.executeQuery("SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner='594317994' ) ORDER BY created DESC LIMIT 1,42");
// Multiple queries
query1 = 'SELECT name FROM user WHERE uid=220439 OR uid=7901103';
query2 = 'SELECT user_id FROM like WHERE object_id=122788341354';
queries = [query1, query2];
queryResults = facebookRestAPI.executeMultiquery(queries=queries);
// Or with nested queries
query1 = 'SELECT uid, rsvp_status FROM event_member WHERE eid=12345678';
query2 = 'SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM ##query1)';
queries = [query1, query2];
queryResults = facebookRestAPI.executeMultiquery(queries=queries);

Facebook ColdFusion Custom tags and XFBML tags

ColdFusion custom tags

The SDK provide a set of ColdFusion custom tags in order to ease development. Those custom tags are executed on the server side by ColdFusion to generate valid Facebook JS SDK code.

Here is the list of available ColdFusion custom tags :

  • html-opengraph-metadata (to insert html opengraph metadata in page head),
  • init (to initialize facebook SDK),
  • invite-link (to display a invite link),
  • login-link (to display a login link),
  • logout-link (to display a logout link),
  • picture (to display a user picture),
  • profile-link (to display a user profile link),
  • publish-link (to display a publish link),
  • share-link (to display a share link).
<cfsilent>
<cfimport taglib="/facebook/sdk/tags" prefix="facebook" />
<!--- Replace this with your appId and secret --->
<cfset APP_ID = "" />
<cfset SECRET_KEY = "" />
<cfset facebookApp = new facebook.sdk.FacebookApp(appId=APP_ID, secretKey=SECRET_KEY) />
<cfset userSession = facebookApp.getUserSession() />
</cfsilent>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <title>Facebook ColdFusion SDK Custom tags Examples</title>
</head>
<body>
<facebook:init facebookApp="#facebookApp#" />
<cfif not structKeyExists(userSession, "uid")>
    <h2>Authentication</h2>
    <facebook:login-link /><br />
<cfelse>
    <h2>Authenticated</h2>
    <facebook:logout-link /><br />
    <facebook:picture facebookId="#userSession.uid#" /><br />
    <facebook:profile-link facebookId="#userSession.uid#" /><br />
    <hr />
    <facebook:share-link label="Share this site" url="http://#cgi.SERVER_NAME#" /> |
    <facebook:publish-link label="Publish to your stream" message="ColdFusion rocks!" /> |
    <facebook:invite-link label="Invite your friend" message="You should try this app" />
</cfif>
</html>

XFBML tags

Facebook provides a set of XFBML tags to insert directly in your HTML page. Those XFBML tags are parsed by the Facebook JS SDK after page load. XFBML parsing is enabled by default in facebook:init ColdFusion custom tag.

Here is the list of available XFBML tags : http://developers.facebook.com/docs/reference/javascript/FB.XFBML.parse/.