Wednesday, December 8, 2010

Create facebook Iframe Application Using flash AS3 API.

A new Updated tutorial have been added,please follow this link below .

http://as3flashcs5.blogspot.com/2011/04/create-facebook-iframe-application.html

please like and post your questions : http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436

After along research on how I can use flash instead of flex to create facebook application by using the new facebook graph API, I finally create a simple class to connect flash with facebook and i am happy to share it with you.

please be aware to follow below steps because any mistake may give you an error .

so as many of you know already know the first step is to create facebook new application from the developer application as you can see in the picture below :

  • when you click setup new application you need to choose your application name and agree the facebook terms ( I choose facetalk) .
  • after your application is created you must know the most important parameters in order to connect with facebook ( API key, application ID, application secret ) .
  • now click the setting and go to the website tap and fill the site url with your application folder URL so for me i fill it with : http://amazingwork.com/amazingtalk/ so i hosted my files inside amazingtalk folder and in the site-domain just fill it with your domain : amazingwork.com .
  • then click the facebook integration tap and now you need to fill the convas url and this is the main URL for your application and every body will access your application thru this URL so for our example I fill it with amazingtalk becuase facetalk is already taken, and in the convas Type please be sure to select Iframe because your application is not related to facebook compiler, u can use any java script , you are not forced to used FBL tags ). finally choose Auto-resize in the iframe size .
at this stage we finish creating our application setting ;) now we will start Writing our code to connect with facebook.

as i found in my research that facebook keep upgrading their code and release new versions of API to allow users to connect with the facebook, so in this tutorial we will connect with facebook using the graph API which the latest version of facebook API.

so since we will use Flash AS3 to connect with facebook you need for sure to install the graph API as3 libraries to connect with facebook, so please click this link below to download this graph API.


after you installed the graph ApI, go to flash cs5 and create new flash AS3, then go to file-->action script setting, then as you can find in the picture below click the + sign and choose the GraphAPI Web_1_0.swc as you can find in the .rar file .

now flash will recognize all the facebook classes .

P.S: if you need to create a desktop version please be aware to download the desktop version from google code :
this will help you to test your API in offline mode before converted to the web version.
also you can download the whole documentation from the google code aslo.

now to make it easy for u i have created simple single tone class that handle the connection with facebook thru flash itself also u can use a java script  to handle the connection :

you can find the code of this class in the source files link in the bottom of this tutorial, so to use this class you need only to copy and paste it in your .fla folder, and then from any place you need to use this class functionality you can write this code :

var oFaceBookConnector:FaceBookConnector = FaceBookConnector.GetFaceBook(Trace_txt);
oFaceBookConnector.APKey = "b1762000d69544ebef65396dfe4a297a"
oFaceBookConnector.APPID = "181390918539668"
oFaceBookConnector.ReDirectURL ="http://amazingwork.com/fbexampletest/index.html"
oFaceBookConnector.addEventListener("Connected",onFaceBookConnectHandler)
oFaceBookConnector.Connect()
function onFaceBookConnectHandler(e)
{
Trace_txt.text = "Connected"
UserID_txt.text = oFaceBookConnector.UserInfo.UserID
}



the Trace_txt is only for testing purpose and you can remove it when you finish your testing.
so as you can notice first of all i get the instant of the facebook object , then i set the importent properties i need to connect to facebook ( APkey, APPID, redirect URL - i use this url to set the permition for the first time user connect to my application)
also i created a custom event to know when my application successfully connected to the facebook .


after we publish the swf  then you need to establish the connection using facebook Javascript graph API, and this can happens using any server pages ( PHP,ASP.net) or even an HTML page .


so after you publish the SWF and HTML page you need to modify the HTML page to connect with facebook.


first of all copy this code in your header tag :
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
    function SetPermissions(ReDirectURL) { 
       top.location.href = ReDirectURL;
       
    }
</script>


in the  body tag and before your SWF object you need to reference to the facebook java script bridge :
<script type="text/javascript" src="FBJSBridge.js"></script>


so by this you finish all itis need to connect to facebook :)
----------------------------------------------------------------------
you can download all you need by follow this link below :



Source Files
Online Demo

if you have any question please feel free to contact me : abed_q@hotmail.com

  










18 comments:

  1. hello!
    Thank's for this post!!!

    How to load facebook profile photo using this?

    ReplyDelete
  2. I mean load the FB profile photo by using the UILoader component

    ReplyDelete
  3. thanks :)

    in order to load user image, u need to get the user image url like below
    first call
    Facebook.api("/me",getMeHandler);
    then
    private function getMeHandler(result:Object,fail:Object):void
    {
    var UserImage:String= Facebook.getImageUrl(result.id)
    //load the image as u want
    }

    ReplyDelete
  4. Hi Abed. Thanks for help, it works :) I'm finaly happy :) I will sen'd you a link wen i finish my App :D

    Cheers!

    ReplyDelete
  5. nice to hear that everything is work fine with u:)

    waiting to see ur APP;)

    ReplyDelete
  6. Hi again. I have one more question... How can i get friend list (uid and the user images url) - I mean list of friends using ur app ;) I've searched and did not found anything working ;/

    I'm almost done - this is my first facebook app, pls help... :)

    ReplyDelete
  7. By the whey - I'm no longer anonymous ;d

    ReplyDelete
  8. hello michal,

    it is nice to know whom i am talking to :)

    in orfer to get ur friend please follow below steps:

    first u need to call facebook graph API for ur friend .
    Facebook.api('/me/friends', handleFriendsLoad);

    then

    function handleFriendsLoad(response:Object, fail:Object):void
    {

    if (fail)
    {
    //name_txt.text = "Error";
    }
    var friends = response as Array;
    var l:int = friends.length;
    for (var i:int=0; i < l; i++)
    {
    trace(friends[i].id);
    }

    }

    it simple ya ?? ;)

    ReplyDelete
  9. Hi Abed,

    Looks like some great work with this project here. I haven't played around with it much yet but I was wondering if this would work with the Facebook Like Box (http://developers.facebook.com/docs/reference/plugins/like-box). What do you think?

    Thanks

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. hi Jesse,

    thanks, yes you can easily add the like box just copy and paste below code and replace the href with your application path:
    P.S:remove the -- sign from iframe tag becuase blogger not allow iframe tag .
    <--iframe src="http://www.facebook.com/widgets/like.php?href=http://www.facebook.com/apps/application.php?id=164745090212453"
    scrolling="no" frameborder="0"
    style="border:none; width:450px; height:80px">

    ReplyDelete
  12. hi Abed,

    i'm back ;)

    I had a lot of work, but now I can take the game. I'l be happy to send you a link to the mail, for example - if you give me one- it is hard to explain to me the game and how you see it you'll know what I mean. I'll have one more question - before I publish it - and will throw the info on your forum

    Cheers! :)

    ReplyDelete
  13. Hi,
    Great tutorial, but its not working for me, i am using your source files under new appid and key, and its keep on saying connecting, it is showing me no error, no clue.. i have followed all steps well..

    please let me know what can be issue.

    here is the URL:
    apps.facebook.com/fconntest/

    ReplyDelete
  14. Hey Abed, let me join the rest by saying what a great tutorial this was. I, however, have the same problem as maani (i get Trace_txt to display connecting but that's all). I too have gotten no error messages. Please help.

    ReplyDelete
  15. thanks alor ricky,

    I have updated my tutorial based on the new API as you can find in my new post :
    http://as3flashcs5.blogspot.com/2011/04/create-facebook-iframe-application.html

    hope now everything will work fine:)

    ReplyDelete
  16. Thanks a lot Abed! I will try it now.

    ReplyDelete
  17. That's a great tutorial in making an application on Facebook. Thanks for sharing it to us.

    ReplyDelete

Create facebook Iframe Application Using flash AS3 API ( Updated Version ).

please like and post your questions :  http://www.facebook.com/pages/FlashCS5-Tutorials/176320862386436 After I have received a lot of requ...