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