Facebook for Adobe AIR

From Facebook Developer Wiki

Jump to: navigation, search

Facebook for Adobe AIR, built on Adobe AIR runtime, allows Facebook Platform desktop applications to interface with the Facebook Platform REST server either through the ActionScript 3.0 Library for Facebook Platform or on its own.

Facebook for Adobe AIR provides your application with the following functionality:

  • A Facebook Connect login mechanism
  • A method to prompt for extended permissions
  • A callMethod function that lets your application call any Facebook Platform API method

Contents

[edit] Getting Started

Facebook Adobe for AIR is available in our public repository. Or, you can download the official Facebook for Adobe AIR application.

To get started using Facebook for Adobe AIR, you need to:

  1. Import Facebook libraries.
  2. Initialize the application with Facebook.
  3. Check the user's login status.
  4. Log the user into your application and, if necessary, Facebook.
  5. Prompt the user for extended permissions.

Then your application can interact with the Facebook REST server.

[edit] Importing Facebook Libraries

In your main ActionScript pages where your Facebook code resides, import the following libraries:

import fb.FBConnect; import fb.FBEvent;


These libraries are required so Facebook Connect authentication and the event listener can operate.

[edit] Initializing Your Application

When the user launches your application, you need to initialize it with Facebook. You need to supply the API key you received when you first configured your application in the Facebook Developer application.

You need to initialize only once, in the applicationComplete callback.

FBConnect.init('YOUR_API_KEY');


[edit] Logging the User In

Once you initialize your application, check for the user's Facebook Connect status. If the user is not connected, call FBConnect.requireSession to authenticate the user:

if (FBConnect.status == FBConnect.NotLoggedIn) { FBConnect.requireSession(); }


If the user is not logged into Facebook, the following dialog appears, allowing the user to both log in to Facebook and connect his or her Facebook account to your application.

[edit] Prompting the User for Extended Permissions

When the user has logged in and connected (that is, FBConnect.status == FBConnect.connected), you can prompt the user for any extended permissions. For information on the extended permissions you might want to prompt for, read http://wiki.developers.facebook.com/index.php/Extended_permissions.

For an application that both reads and writes to a user's stream, you should prompt the user for the read_stream and publish_stream extended permissions.

First, check if the user already granted your application the extended permission. For example, if your application requires the user to have the read_stream and publish_stream permissions, check for them using FBConnect.hasPermission.

If the user doesn't have the permissions, then prompt for them with FBConnect.requirePermissions. Include all the permissions you require in an array, as in:

if (FBConnect.hasPermission("read_stream") && FBConnect.hasPermission("publish_stream")) doSomething(); else FBConnect.requirePermissions(["read_stream", "publish_stream"]);


The library contains an event listener that determines if the user's Facebook Connect status or extended permissions change. The listener, FBConnect.dispatcher, sends out one of two events:

  • FBEvent.STATUS_CHANGED, which indicates that the user's Facebook Connect status has changed
  • FBEvent.PERMISSION_CHANGED, which indicates that the user's extended permissions have changed

[edit] Calling the Platform API

Once the user has logged into Facebook and connected accounts, you can start making calls to the REST server using the ActionScript 3.0 Library for Facebook Platform. Or you can call the REST server directly using the FBAPI.callMethod function in this library.

For more information and to download the ActionScript 3.0 library, visit the Adobe website.

If you're using FBAPI.callMethod, the only parameters you need to include are method and args.

  • method is a string in which you specify any Facebook API method, like friends.get.
  • args: is an object containing key/value pairs of arguments to include for the API call.

Note: Standard parameters like v, format, api_key, call_id, and session_key are all included for you automatically.

The method returns an EventDispatcher, which in turn dispatches one of two events from the server:

  • FBEvent.SUCCESS, if FBAPI.callMethod executes successfully. The FBEvent.SUCCESS event contains a .data object, which contains the JSON-decoded results of the call to the REST server (like user data, for example).
  • FBEvent.FAILURE, if the method fails.

[edit] Example Requests

The following examples illustrate how to call the Facebook REST server using FBAPI.callMethod. For examples using the ActionScript library, see http://www.adobe.com/devnet/facebook/#startnow.

For example, if you want to get the list of the user's friends, call friends.get:

FBAPI.callMethod("friends.get").addEventListener(FBEvent.SUCCESS, function(event:FBEvent):void { trace("All friend IDs are: " + event.data.join(",")) });

If you want a user to tag photo 4145 with user 1681,

FBAPI.callMethod("photos.addTag", {pid:4145, tag_uid:1681, x:40, y:20});

Note: In the second example, we're not using addEventListener to check the result, since the method simply returns 1 if successful.

reference