Facebook Connect + Coldfusion Using the Graph API

by Michael Santoroski on September 2, 2011

The new graphapi makes getting Facebook connect super easy, but putting all the pieces together is a little tough.

What you need to do is set-up the Facebook connect button, and let it save the cookies for you. From there you can get an access token, required to get information back from the API. You can use cfhttp calls and get JSON back.

Step one is to follow Facebook’s instructions to get the Javascript set-up for the Facebook Connect button, found here:

https://developers.facebook.com/docs/guides/web/#login

Note: You need to request the permissions that you want in the button HTML, for example ours looks like this:

<fb:login-button perms="email, user_education_history, friends_education_history, publish_stream, offline_access">

Once this is done, you can use some ColdFusion to read the cookie and get the accesstoken.

<cfset left = Find("access_token=",cookie.fbs_239625941285)+13>
  
<cfset right = Find("&",cookie.fbs_239625941285,left+1)>
<cfset accessToken = Mid(cookie.fbs_239625941285,left,right-left)>
<cfoutput>Access Token = #accessToken#</cfoutput>

And to get the Facebook ID:

cfset left = Find("&uid=",cookie.fbs_239625941285)+5>
<cfset len = len(cookie.fbs_239625941285)-left>
<cfset SESSION.FBID = mid(cookie.fbs_239625941285,left,len)>
<br><cfoutput>Facebook ID = #SESSION.FBID#</cfoutput>

The next thing is to use the Graph Explorer tool to find the syntax for what you want to pull:

https://developers.facebook.com/tools/explorer

For example to get e-mail we use the code:

<cfdump var="#cookie#">
<cfhttp url="https://graph.facebook.com/100000929135466/?#accessToken#" method="get" />
        <cfset response = DeserializeJSON(cfhttp.filecontent) />
<cfoutput>
<p>email = #response.email#</p>
</cfoutput>

You can try an example here:

Example

And I have uploaded the CFM files here:

http://webapps.roanoke.edu/michael/fbconnect/facebookconnect.zip

Let me know if you have questions or comments!

{ 5 comments… read them below or add one }

Evik James December 19, 2011 at 9:54 am

I can’t get your example to work, nor can I get your code to work on my page. It seems it has the same issue: nothing happens. You do, however, have less cryptic code than other people and I would like to get yours working. Can you confirm that your example works as it’s supposed to? What should happen when I click this button? Nothing happens for me, whether I am logged into Facebook or not. http://webapps.roanoke.edu/michael/fbconnect/logintest.cfm

Serge March 7, 2012 at 1:52 am

Hi Evik! I can’t make it work also. Did you find solution?

Michael Santoroski March 7, 2012 at 8:36 am

I will take a look at see what is going on.

rachel November 20, 2012 at 4:49 pm

It works, you just have to replace the “secret_key” and “appID” with your owns, you can delete “api_key”, I did not use it.

Mosey February 28, 2013 at 6:05 pm

I tested this and am just getting the blank page.

I have other methods just with pure JS that work fine. Looking for a CF solution.

Leave a Comment