Creating a Flash Facebook application with the Facebook Actionscript API – part 2

It’s time to make some considerations on the Facebook application made with the Facebook Actionscript API I blogged some days ago.

There was a doubt about putting a secret key in the swf file.

Well, you should never give away your secret key, but let me point on two things:

1) You can encrypt your swf file, obfuscating the secret key… and just in case you think nothing is encrypted enough… surprise…

2) My Facebook application works without api and secret keys. That’s it… I tested it with more than one account, and everybody was able to use the application even if my keys are stored this way:

var api_key:String="xxxxxxxxxxxxxxxxxxxxxxx";
var secret_key:String="yyyyyyyyyyyyyyyyyyyyyyy";

I am not hiding them, they are really a series of x and y… probably when you render a swf in canvas mode, they aren’t mandatory.

I have to say, I did not find docs about it, so take it as it comes.

The feature introduce this time is showing all your friends… there was a reader that was unable to populate a list with friend names, so here it is.

I didn’t create any list, but I’m simply showing them in a text area.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package {
	import flash.display.Sprite;
	import fl.controls.Button;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	// facebook libraries
	import com.facebook.Facebook;
	import com.facebook.utils.FacebookSessionUtil;
	import com.facebook.commands.notifications.*;
	import com.facebook.commands.friends.*;
	import com.facebook.commands.stream.*;
	import com.facebook.commands.users.*;
	import com.facebook.events.*;
	import com.facebook.data.users.*;
	import com.facebook.data.friends.*;
	import com.facebook.net.FacebookCall;
 
	public class facebook_demo extends Sprite {
		var vars_field:TextField=new TextField  ;
		var friends_field:TextField=new TextField  ;
		var my_button:Button;
		var fbook:Facebook;
		// these are the api and secret keys you should be used to
		var api_key:String="xxxxxxxxxxxxxxxxxxxxxxx";
		var secret_key:String="yyyyyyyyyyyyyyyyyyyyyyy";
		// starting a new facebook session
		var fb_session:FacebookSessionUtil=new FacebookSessionUtil(api_key,secret_key,loaderInfo);
		// in this object I will store all variables Facebook will pass to the movie
		var passed_vars:Object;
		// this variable will hold all facebook API calls
		var fbcall:FacebookCall;
		public function facebook_demo() {
			// just placing some buttons...
			place_buttons();
			// just creating a text field...
			create_vars_field();
			// initializing the session
			fbook=fb_session.facebook;
			// this function will just print out all variables Facebook is passing to the movie
			get_vars();
			// getting friends uids (unique ids)
			fbcall = new GetFriends();
			fbook.post(fbcall);
			// function on_get_friends will be called once the post is completed
			fbcall.addEventListener(FacebookEvent.COMPLETE, on_get_friends);
		}
		// function to be called after GetFriends has finished
		function on_get_friends(e:FacebookEvent):void {
			var friends = (e.data as GetFriendsData).friends;
			var friends_num:int=friends.length;
			var uids:Array = new Array();
			// loading all friends uids into an array
			for (var i:int=0; i<friends_num; i++) {
				var user=friends.getItemAt(i) as FacebookUser;
				uids.push(user.uid);
			}
			// retrieving informations for all uids contained in the uids array
			fbcall=new GetInfo(uids,[GetInfoFieldValues.ALL_VALUES]);
			fbook.post(fbcall);
			// the function on_friend_list_loaded will be called once the post is completed
			fbcall.addEventListener(FacebookEvent.COMPLETE, on_friend_list_loaded);
		}
		// function to be called after GetInfo has finished
		function on_friend_list_loaded(e:FacebookEvent) {
			var friends_data = ((e.data as GetInfoData).userCollection);
			var friends_num=friends_data.length;
			// scanning all friends array and printing name and last name of each one
			for (var i:int=0; i<friends_num; i++) {
				var user=friends_data.getItemAt(i);
				friends_field.appendText(user.first_name+" "+user.last_name+"\n");
			}
		}
		// this is the core function, the one that will handle clicks on buttons and publising notes or stories according
		// to the button I am pressing
		function onclick(event:MouseEvent):void {
			// retrieving the user id (it's one of the variables passed by Facebook)
			var uid:Number=Number(passed_vars["fb_sig_user"]);
			switch (event.currentTarget.label) {
				case "Publish a note" :
					// publishing a note
					fbcall=new SendNotification([uid],"just sent a self-notification using <a href = \"http://apps.facebook.com/flash_demo/\">Facebook Flash Demo</a>","user_to_user");
					fbook.post(fbcall);
					break;
				case "Publish a story" :
					// publishing a story
					fbcall=new PublishPost("is using Facebook Flash Demo",{'href':'http://apps.facebook.com/flash_demo/','name':'Facebook Flash Demo','description':'that\'s it','caption':'this image was published by a Flash Movie','media':[{'type': 'image','src':  'http://www.gamemummy.com/facebook/flash_demo/api.jpg', 'href': 'http://apps.facebook.com/flash_demo/'}]},[{'href':'http://apps.facebook.com/flash_demo/','text':'Visit Facebook Flash Demo'}],uid.toString());
					fbook.post(fbcall);
					break;
			}
		}
		function create_vars_field():void {
			addChild(vars_field);
			vars_field.width=340;
			vars_field.height=320;
			vars_field.x=0;
			vars_field.y=10;
			vars_field.text="DEFAULT VARS PASSED BY FACEBOOK:\n\n";
			addChild(friends_field);
			friends_field.width=340;
			friends_field.height=320;
			friends_field.x=350;
			friends_field.y=10;
			friends_field.text="YOUR FACEBOOK FRIENDS (scroll with mousewheel):\n\n";
		}
		function get_vars():void {
			var varname:String;
			var varvalue:String;
			passed_vars=root.loaderInfo.parameters;
			for (varname in passed_vars) {
				varvalue=String(passed_vars[varname]);
				vars_field.appendText(varname+":\t"+varvalue+"\n");
			}
		}
		function place_buttons():void {
			var bnames:Array=new Array("Publish a note","Publish a story");
			for (var i:int=0; i<2; i++) {
				my_button=new Button  ;
				addChild(my_button);
				my_button.label=bnames[i];
				my_button.move(225+150*i,350);
				my_button.addEventListener(MouseEvent.CLICK,onclick);
			}
		}
	}
}

The result as usual is on the application page and there is no need to download anything since you can just try it by just cutting/pasting the code on the example you can find at this link.

Can you all make the application work without api and secret keys?

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 3.13 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

8 Responses

  1. **Maybe** thats because the AS3 FB lib get these variables from the flashvars that the tag deliver.

    I’m back =]

  2. **EDIT** Well the facebook doesnt pass the API secret key, but I’ve heard that there are functions that NOT requiring the secret key. The API key is passed as well via the flash vars, so the library takes it from there.

  3. [...] Creating a Flash Facebook application with the Facebook Actionscript API – part 2 [...]

  4. Karim AS says:

    The friends column is empty in my app. As I was testing it too I kept receiving an error.
    The error was mainly due to this line:
    fbcall=new GetInfo(uids,[GetInfoFieldValues.ALL_VALUES]);

    It was returning empty data in the event listener function.

    I replaced the above line by:
    fbcall=new GetInfo(uids,['first_name', 'last_name']);
    and it worked.
    Nice tutorials! :)

  5. You’re correct. You don’t have to put the API Key and API Secret Key in your source because the API Key and Secret Session (Some encrypted form of your Secret Key) are passed through flashVars IF and ONLY IF the SWF your are loading into your canvas is on the same domain as your Canvas Calback URL.

    Otherwise, you have to put them in your code.

    There is another way to retrieve those items because you have to do it for desktop applications using the Facebook framework (unless you put your keys in your code *don’t do that*). However, I haven’t tested them.

  6. Jakub says:

    I cannot make my flash open “publish” popup.

    I’ve got this code in my flex application:

    session=new FacebookSessionUtil(“api”,”secret”, loaderInfo);
    fbook = session.facebook;
    if(loaderInfo.parameters.fb_sig_session_key){ session.verifySession(); session.validateLogin(); } else{ session.login();
    }
    var message:String = “Hello world”;

    var attachment:Object = {media: [{
    type: "image",
    src: "http://domain.com/mtunes.jpg",
    href: "http://domain.com/"
    }]};

    var actionLinkData:ActionLinkData = new ActionLinkData();
    actionLinkData.href = “http://domain.com/”;
    actionLinkData.text = “Test text”;
    fbook.refreshSession();
    session.validateLogin();
    session.verifySession();
    var post:PublishPost = new PublishPost(message, attachment, [actionLinkData], fbook.uid.toString());
    var call:FacebookCall = fbook.post(post);

    —————

    call.success returns false, and call.error is null.

    I’m not sure how to make this work.

  7. James Star says:

    Hi,

    I am currently working on a facebook web app, and I noticed that there is a param for offline_access, e.g. login(true). Do you know of a way to implement this feature using actionScript?

    Thanks, James

  8. Jose says:

    Hi emanuele! Congrats on your hard work i have learnt a lot from you.

    When i try your example this is what i get, i am getting mad at this error, do you know what can be? thnks in advance:

    http://api.facebook.com/restserver.php?v=1.0&api_key=xxxxxxxxxxxxxxxxxxxxxxx&call_id=12705696125270&method=friends.get&sig=a54517f3bc002831aaacc90d77c3feb8
    TypeError: Error #1009: No se puede acceder a una propiedad o a un método de una referencia a un objeto nulo.
    at facebook_demo/on_get_friends()

Leave a Reply