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.

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; iFacebook Flash Demo","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?

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers