Skip to main content

Implementation of Facebook Graph API in Coldfusion

Facebook has launched a new FB API called Graph that simplifies FB applications development, the new Graph API allows website owners to create Single Sign On (SSO) with Facebook and allow websites owners to be able to import a lot of users information but after their permissions.

The code below written in Coldfusion gives an example on how to Create FB login/logout button and then how to retrieve the created cookie, and use it for further development:
    <!--- Your FB application IDS --->
    <cfset api_key = "XXXXXXXXXXXXX"/>
    <cfset secret_key = "XXXXXXXXXXXXXX"/>
    <cfset appID = "XXXXXXXXXXXXXX"/>
    <!--- Facebook login/logout button --->
    <p><fb:login-button perms="email,user_birthday" autologoutlink="true"></fb:login-button></p>
    <!--- Facebook login/logout button that publishes to your wall--->
    <!--- Facebook script that connects your site to facebook and creates the needed cookies after authentication --->
    <div id="fb-root"></div>
      window.fbAsyncInit = function() {
        FB.init({appId: '#appID#', status: true, cookie: true,
                 xfbml: true});
      (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol +
        e.async = true;
    <!--- End Facebook script that connects your site to facebook and creates the needed cookies after authentication. Now a cookie is created with a name that starts with fbs_ApplicationID --->

    <!--- Function that extracts User ID and access token from FB cookie. Both access_token and user id are needed to extract information related to the user from FaceBook Graph API --->
    <cffunction name="get_facebook_cookie" access="public" output="true" returntype="any">
        <cfargument name="app_id" type="string" required="true">
        <cfargument name="application_secret" type="string" required="true">
        <cfargument name="charToFind" type="string" required="true">
        <cfargument name="chatToRemoveFront" type="string" required="true">
        <cfargument name="chatToRemoveEnd" type="string" required="true">
        <cfset args=arrayNew(1)>
        <cfset x=StructFind(cookie, trim('fbs_'&'#app_id#'))>
        <cfset args=listtoarray(x,'&')>
        <cfloop from="1" to="#arrayLen(args)#" index="i">
            <cfif FindNoCase("#charToFind#",args[i])>
                <cfset rvalue=#right(args[i],(len(""&args[i])-chatToRemoveFront))#>
                <cfset rvalue=#left(rvalue,(len(""&rvalue)-chatToRemoveEnd))#>
                <cfreturn rvalue />
        <cfcatch type="any">
        <cfreturn 0 />

    <cfset uid=get_facebook_cookie(appID,secret_key,"uid=",4,1)>
    <cfset access_token=get_facebook_cookie(appID,secret_key,"access_token=",14,0)>
    <!--- Get FB User Profile. Profile Information allowed by the user are stored in a struct after deserialized from JSON --->
    <cfhttp url="" result="profile"/>
    <cfif IsJSON(profile.filecontent)>
        <cfset prof=DeserializeJSON(profile.filecontent)>
        <cfdump var="#prof#">
    <!--- Get FB User Friends.
Friends Information allowed by the user are stored in a struct after deserialized from JSON--->
    <cfhttp url="" result="friends"/>
    <cfif IsJSON(friends.filecontent)>
        <cfset friendsl=DeserializeJSON(friends.filecontent)>
        <cfdump var="#friendsl#">



  1. Thanks this is great, however I am getting the following error:

    Error validating application.

    Any suggestions?

  2. What's weird is that if I use this code to generate the token, and then manually lookup this url in my browser, it works...but ONLY if i have TWO (2) question marks in there,.. strange!!!<>??access_token=<>

    ^This works in the browser but WONT work via cfhttp!!

    How is that possible?

  3. aha, i was able to get it working by changing a few settings:

    the numbers you were using to measure the strings were off by 1 on each variable!

  4. For the Anonymous, this error means that the cookie does not exist on your webserver anymore, you have to refresh the page to recreate the cookie. To sort out this create an exception handling and in the catch refresh the page.

  5. Great Dental, what is the browser your using?

  6. I got it to work and created an open source project based on your work:

  7. Yes I noticed that. Thank you Great Job. Actually I tested my code on firefox and explorer and it is working, i needs minor enhancement to check if the cookie exists, also I will add some lines for publishing on wall. Let us try to collaborate on this. Have u tested the code on chrome?

  8. Sounds good, send me your email via this conect form:

    We can then discuss further collaboration on development.

  9. With me both worked, what version of explorer you used?

  10. struct error:

    message Error processing access token.
    type OAuthException

    i can see buttons login and like, but after that show the error

    1. the same error : Invalid token
      Did you solve the problem?

  11. Create exception handling cftry to catch the exception when the user is not authenticated, if you want me to do it for you let me know.

  12. This is a great help, thanks for posting it. It did not work for me as well but all I had to do was remove the "me" part of the first CFHTTP url and the "me/" part of the second CFHTTP url.

    Do you know did the API change or is there some functionality of the "me" part of the URL that I am missing?

    Thanks again for posting.

  13. I have tested the code before I published it, "me" directs graph API to publish to your wall. Anyway there is a newer post that has a full example on how to publish to your wall and to your friends walls as well. Thank you for your feedback.

  14. I'm getting:

    Security: The requested template has been denied access to createobject(java)

    Seems to happen upon outputting the results of the cfhttp request

    I get the access token and id with no problem but when its sent over using cfhttp it seems not to authenticate.

  15. Omar,

    Please try to use the example in the below URL it is newer and 100% working:

  16. Jesus MORE old FB code that doesn't work. I'm wondering exactly what the problem is. Does Facebook just make changes so fast that nobody can keep up? Is the API so delicate that code working on one server needs major mystery tweaking so that it will work on a second? Where did the FB "API Key" go? Can't find it on Facebook Developer page although there is a hint that it's the same thing as the APP ID except it never works either.

    Hasn't ANYONE got a nice stable implementation of the Facebook API in Coldfusion?

  17. Please identify what your problem is? did you try to implement the code and it did not work? as I can see you were not able to find the API keys, if this is the case, please search FB to find the keys. If the code is not working please send me the error your getting.

    I'm sharing my code for free and it was tested and worked on different browsers.

  18. first and second cfhttp calls should be AND respectively

  19. this is very good and running nice.


Post a Comment

Popular posts from this blog

Top Google Adsense Alternatives

Google Adsense is a web tool that allows publishers in the Google Network of content sites to automatically serve text, image, video, and rich media adverts that are targeted to site content and audience. These adverts are administered, sorted, and maintained by Google, and they can generate revenue on either a per-click or per-impression basis.  Google servers advertisers using google adwords platform, while adsense is the publishers platform. Google Adsense is the top Ad Publishers platform over the web ranking number one in web advertising industry. Adsense offers contextual advertisements that covers web sites, blogs, games, videos, mobile browsing etc. What made Google Adsense no. 1 is the reliability, stability, variety of services and large number of publishers including google it self. Also google has a fair platform that detects invalid clicks so google successfully protects its advertisers and also offers its best publishers top CPC. Two reasons are behind people think

Error: Write to Disk Access Denied - Troubleshooting - BitTorrent

I have downloaded Bit Torrent software and when trying to download I got an error after few seconds saying: Error: Write to Disk Access Denied Solving this problem is so simple: Shut down BitTorrent program. Go to Start and in the small search box on top of windows start button start typing Bittorrent and the program will show, right click with the mouse on the icon and Run as Administrator. All ur problems are sorted out now and you can enjoy downloading... Good Luck.

How to Build RSS 2.0 Feed in Coldfusion 8

In this tutorial I will show some tips on how to create an RSS Feed in coldfusion by querying a Database. This example will demonstrate how to create an RSS Feed for the quote of the day. < cfquery name="qname" datasource="dbsource" maxrows="3"> SELECT PUBDATE,DESCR,FTITLE,LINK FROM wiki.url.rssfeed order by id desc </cfquery> <!--- Note here the query returns only the last three quotes and also it is sorted based on the id which is an auto increment ID.---> <cfscript> myStruct = StructNew(); = ""; myStruct.title = "Mustafa Abusalah Quote of the Day"; mystruct.description = "Quote of the Day Tutorial"; mystruct.pubDate = Now(); mystruct.version = "rss_2.0"; <!--- Now the Query columns must be mapped to the RSS columns this is done by creating a struct and mapping them to the struct variables. Please not that the fields must be written in capital letter