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

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.

Coldfusion Facebook Graph API publish to your wall and your friends walls

In this tutorial we will learn by full coldfusion Graph API code example how to publish on your wall and your friends walls. This application uses new oauth authentication method. The code is divided into four files: we will first start with a file called index.cfm: <cfoutput>         <!--- Your FB application IDS --->       <cfset api_key = ""/>     <cfset secret_key = ""/>     <cfset appID = ""/>     <!--- create a connection to the fb graph cfc --->     <cfset graphCFC = createObject("component", "graph").init(#appID#, #api_key#, #secret_key#) />     <!--- If user is authenticated or his access token is set create a cookie --->        <cfif not isdefined("cookie.access_token") and isdefined("url.access_token")>         <cfset cookie.access_token=url.access_token>     </cfif>     <!--- If the user is authenticated and access token

IDLE subprocess didn't make connection Python 3.7

After installing Python on Windows 10. When trying to open IDLE, Python's IDE, you might get a  message saying that " IDLE's subprocess didn't make connection . Either IDLE can't start a subprocess or personal firewall software is blocking the connection ". To solve this issue just run IDLE as an Administrator, by right click on the IDLE icon and click Run as administrator. See Photo below: