Skip to main content

Authorization with Facebook Using Graph API and Coldfusion

Below is an alternative Coldfusion Facebook Authorization method to the earlier one that was published yesterday. This method is simpler and does not require much code.


You should create two file, one that will redirect the user of your application to Facebook API to get authenticated and then authorized, the first file is below:

    <!--- Your FB application IDS --->
    <cfset api_key = "xxxxxxx"/>
    <cfset secret_key = "xxxxxx"/>
    <cfset appID = "xxxxxx"/>
    <!--- Redirect to get authonticated and authrorized from facebook, in the redirect_uri you should write the url of your website where you want the user to be redirected after authroization --->
   <cflocation url="https://graph.facebook.com/oauth/authorize?client_id=#appID#&redirect_uri=http://www.sms4all.info/fb/login1.cfm" addtoken="no">


The second coldfusion file is the main page where the user will be redirected to, as an example I only retrieved the friends list of the authorized user:


     <cfset api_key = "xxxxxx"/>
    <cfset secret_key = "xxxxxx"/>
    <cfset appID = "xxxxx"/>
    <!--- Fetching the below URL will return to the application the access token which allows your application to access the users information --->

    <cfhttp url="https://graph.facebook.com/oauth/access_token" result="login1">
    <cfhttpparam name="client_id" value="#appID#" encoded="no" type="url">
    <cfhttpparam name="redirect_uri" value="http://www.sms4all.info/fb/login1.cfm" encoded="no" type="url">
    <cfhttpparam name="client_secret" value="#secret_key#" encoded="no" type="url">
    <cfhttpparam name="code" value="#url.code#" encoded="no" type="url">
    </cfhttp>
    <cfset accesstoken=login1.filecontent>
    <!--- Get FB User Friends --->
    <cfhttp url="https://graph.facebook.com/me/friends?#accesstoken#" result="friends"/>
    <cfif IsJSON(friends.filecontent)>
        <cfset friendsl=DeserializeJSON(friends.filecontent)>
        <cfdump var="#friendsl#">
    </cfif>

Comments

  1. Works great!! Thanks so much for sharing.

    ReplyDelete
  2. Thanks, it works great!!

    ReplyDelete
  3. Mustafa, Thank you for this clear and easy to understand tutorial! :)

    ReplyDelete
  4. Silly question but I've been trying for a few hours. How do you output the JSON without dumping it so that it can be formatted with HTML .

    ReplyDelete
  5. Dear Omar, It is so easy you need to deserialize the JSON variable, then dump it to see the structure and if it has arrays. Then directly you may get the result easily by calling the struct and its sub-structs or arrays.

    ReplyDelete
  6. Can I request the user's email with this method?

    ReplyDelete
    Replies
    1. Please read the below post:
      http://blog.abusalah.info/2010/08/coldfusion-facebook-graph-api-publish.html

      and a list of permissions on the below post:
      https://developers.facebook.com/docs/reference/login/#permissions

      Delete

Post a comment

Popular posts from this blog

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 is set, then make the action…

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.

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:

  <body>
  <cfoutput>
<!--- 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--->
    <p><fb:like></fb:like…