Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite 6
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions
  • Government

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection / Gaming /

Getting started with the Facebook SDK and ActionScript 3

by Tom Krcha

Tom Krcha
  • Adobe
  • gamingnotes.com

Content

  • Step 1: Set up communication between Flash and JavaScript
  • Step 2: Set up a development server for the Flash app
  • Step 3: Create a new Facebook app
  • Step 4: Initialize the Facebook JS SDK
  • Step 5: Login to Facebook and get some data
  • Where to go from here

Created

5 February 2013

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScriptFlash Buildergame developmentgamingJavaScript
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

Requirements

Prerequisite knowledge

Experience with JavaScript, ActionScript, and Flash Builder, as well as some familiarity with Facebook will help you make the most of this article.

User level

Intermediate

Required products

  • Flash Builder (Download trial)

Sample files

  • FacebookDemo.zip

When you develop and deploy an app or game on Facebook you need to invoke the Facebook API to login as well as to get details about the user, friends, photos, and other information. There are currently several official SDKs that you can use to perform such tasks via the Facebook Graph API. This article will guide you through the basics of using the JavaScript SDK with ActionScript 3.

The Facebook API is simple and supports rapid extensibility without API changes, via the Graph API.

A good place to begin is with these three Facebook JavaScript API calls:

  • FB.init – initialize Facebook API
  • FB.login - login to Facebook
  • FB.api - perform a Graph API call on Facebook

You can call the JavaScript wrappers from ActionScript using flash.external.ExternalInterface. This provides access to the Facebook API from the Flash runtime and enables you to be always up-to-date whenever Facebook changes or updates their official JavaScript APIs (see Figure 1).

Figure 1. Accessing Facebook from ActionScript via the JavaScript SDK.
Figure 1. Accessing Facebook from ActionScript via the JavaScript SDK.

To try this out yourself, you need to complete following steps, which are outlined below:

  • Set up communication between Flash and JavaScript
  • Set up a development server for the Flash app
  • Create a new Facebook app
  • Initialize the Facebook JavaScript SDK
  • Login to Facebook and get some data

Step 1: Set up communication between Flash and JavaScript

To call the Facebook JavaScript SDK from the Flash runtime, you first need to establish communication between the two layers using ExternalInterface.

  1. Add these three functions into your main ActionScript 3 class and call the init() function from the constructor:
private function init():void{ ExternalInterface.addCallback("myFlashcall",myFlashcall); stage.addEventListener(MouseEvent.CLICK, onClick); } private function myFlashcall(str:String):void{ trace("myFlashcall: "+str); } protected function onClick(event:MouseEvent):void{ if(ExternalInterface.available){ trace("onClick"); ExternalInterface.call("myFBcall"); } }
  1. Then add this code to your HTML file inside the <head> element (if you are using Flash Builder, add it to index.template.html):
<script> function myFBcall(){ alert("test"); // should work in every modern browser document.getElementById("NAME_OF_YOUR_FLASH_ELEMENT").myFlashcall(“test string”); } </script>

Replace NAME_OF_YOUR_FLASH_ELEMENT with an actual name, for example the name of your main class. I used "FacebookDemo" (my main class is named FacebookDemo.as).

  1. Run the app. If you click anywhere on the stage inside the Flash content, it should make a JavaScript call, display an alert, and call back into ActionScript to trace a string.

Step 2: Set up a development server for the Flash app

You can develop your app online or set up a server on localhost. I prefer using localhost for development. On Mac OS X you can use the MAMP server, and on Windows you can use the WAMP server.

My MAMP server is set up with port 8888 and the web root is /Applications/MAMP/htdocs.

  1. Add a fbdemo folder in the htdocs folder.
  2. To simplify the development process, configure Flash Builder to compile into your htdocs/fbdemo folder and run (or debug) the app on your server (see Figure 2).
Figure 2. Configuring the Output Folder and Output Folder URL in Flash Builder.
Figure 2. Configuring the Output Folder and Output Folder URL in Flash Builder.
  1. Verify that your app runs from your server.

Step 3: Create a new Facebook app

The next step is to create a Facebook app.

  1. Navigate to https://developers.facebook.com/apps/ and click Create New App. 
  2. For the App Domain, type localhost.
  3. Select Website With Facebook Login, and type your Site URL, http://localhost:8888/fbdemo (see Figure 3).
  4. Once your app is created, remember the API key/App ID. You will need it next.
Figure 3. Creating a new Facebook app.
Figure 3. Creating a new Facebook app.

Step 4: Initialize the Facebook JS SDK

For details on the Facebook JavaScript SDK, visit https://developers.facebook.com/docs/reference/javascript/.

  1. To initialize the SDK, include the following code within your HTML <head> element and replace YOUR_APP_ID with the App ID you noted in the previous step. (If you are using Flash Builder, add this code to index.template.html.)
<script> window.fbAsyncInit = function() { // init the FB JS SDK FB.init({ appId : 'YOUR_APP_ID', // App ID from the App Dashboard channelUrl : 'www.YOUR_DOMAIN.COM/channel.html', //Channel File for x-domain communication, replace YOUR_DOMAIN.COM with your domain status : true, // check the login status upon init? cookie : true, // set sessions cookies to allow your server to access the session? xfbml : true // parse XFBML tags on this page? }); // Additional initialization code such as adding Event Listeners goes here }; // Load the SDK's source Asynchronously // Note that the debug version is being actively developed and // might contain some type checks that are overly strict. // Please report such bugs using the bugs tool. (function(d, debug){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; ref.parentNode.insertBefore(js, ref); }(document, /*debug*/ false)); </script>
  1. Next add a fb-root div to your <body> element.

<div id="fb-root"></div>

Step 5: Login to Facebook and get some data

Now you’re ready to login to Facebook and start making API calls to retrieve data.

  1. Edit the myFBcall function, and replace its contents with the following code. This code will login to Facebook, fetch your name, display it in an alert, and send it back to the Flash runtime.
<script> function myFBcall(){ FB.login(function(response) { if (response.authResponse) { console.log('Welcome! Fetching your information.... '); FB.api('/me', function(response) { alert('Your name is ' + response.name); document.getElementById("FacebookDemo").myFlashcall(response.name); }); } else { console.log('User cancelled login or did not fully authorize.'); } }); } </script>

Now you have set up basic communication from Flash Player to JavaScript to Facebook and back.

The code above calls the FB.login function and creates an anonymous function callback, which checks for the actual authResponse status. It then calls FB.api function, which calls the Facebook Graph API. To get information about the current user, it simply passes '/me' as a parameter.

Once you have logged in, you can just call FB.api("command") alone without the need for FB.login; for example:

function getProfilePicture(){ FB.api("/me/picture", function(response) { // DO SOMETHING }); }

To verify that you are currently logged in, you can call FB.getLoginStatus.

Refer to the developer documentation on Facebook for more information.

Where to go from here

To understand the Graph API better and test your commands before you deploy them, use the Graph API Explorer.

You should also check out the Graph API documentation, which has all the information you need to properly form your FB.api commands.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Products

  • Adobe Creative Cloud
  • Creative Suite 6
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy (Updated) | Cookies

Ad Choices