|
|
Flex ActionScript 3.0 example
Follow these steps to create the Social Hello World application
using ActionScript 3.0 in Flex. These steps are repeated as comments
in the example code that follows to help you implement them.
Include the fps.as file in your project.
Download
the fps.as file from http://help.adobe.com/en_US/FPS/Social/GettingStarted-AS3.zip.
Unzip the file and include it in your project by placing the file
in a subfolder named com/adobe off of your src directory or any
other root folder of your classPath. This file provides bootstrapping
code for the Social API, which you should not change. Get the latest
version of this file whenever you change your project so that you
include the latest improvements and fixes.
You also must import
the corresponding class by adding the following import statement
to your ActionScript code: import com.adobe.fps;
Note: The
fps.as file implicitly gives the Social API code permission to access
your SWF file's data members. If you use the Social API in a SWF
file that is loaded by another SWF file, or within a hierarchy of
other SWFs, all the parent SWFs must give Social permission to access
them as well. You can implement that by adding the following lines
of code:
Security.allowDomain("cdn.gigya.com");
Create a global configuration (Conf) object.
Every
Social API method requires a Conf (configuration) object as its
first parameter. This object stores global configuration parameters
that are common to all of the API calls and that are expected to
remain static for the duration of the application. It is a best
practice to define one global Conf object and use it throughout
the application.
Place your API Key in the Conf object where
you see the string “Put your API Key here”. You can obtain an API
Key http://www.adobe.com/go/social_setup.
The
example uses the following three members of the Conf object: - APIKey
- The API key provided by the Social API page. The APIKey is
a required configuration parameter.
- mcRoot
- This parameter refers to the root of your Flash container;
it is a required configuration parameter.
- enabledProviders
- A comma-separated list of providers that will be enabled in
Social. This is an optional configuration parameter. Use '*' to
enable all the providers.
Note: The mcRoot parameter
is not initialized statically. It is initialized inside the init() method
because we set the mcRoot parameter with the value this.root,
which cannot be accessed statically. Refer to the following lines
in the example code:
// Initialize the mcRoot attribute of the Conf object to the root of the Flash
// container.
this.conf.mcRoot=this.root;
Load the Social service.
Load the Social service before
using the Social API. Load the Social service by executing the fps.load(conf, params) method
and use your callback method to determine whether the load was successful.
Do not call any other Social API method before your call your callback
method and determine whether the load was successful. Create a Params object to use as the params parameter
for fps.load().
The Params object includes
the specific parameters sent to the API method. The fps.load() method
expects the following members in the Params object, which are included
by the loadParams object in the example code: services - (Required). A comma-separated
list of service names that you want to use in your application.
For Social, set this parameter with the string 'socialize'. For
information about other services, see the Social API Reference.
callback - (Optional). A reference to the
callback function. Social calls the specified function with the
results when the API method completes.
context - (Optional). A developer-created
object that is passed back unchanged as one of the fields in the
Response object.
Execute the fps.load() method.
Call
the fps.load() method, passing the Conf and Params
objects as parameters.
Define a callback function for the load() method.
Recall
that we specified the callback function in the callback attribute
of the params object: callback:onServiceLoad.
Therefore, when we call the fps.load() method,
the onServiceLoad() function is called when the
load processing completes.
You must define the callback function.
The only constraint is that the Social API expects the callback
function to receive one parameter, which is the Response object.
The Response object contains the values returned from the API method.
The callback function should analyze the Response object and handle
it appropriately. For example, it could display an error message to
the user, if an error occurred. The Response object for the fps.load() method
includes the following members:
hadError -
a Boolean indicating whether an error occurred
errorCode - an error code or 0 if no error
has occurred
errorMessage - a string containing the description
of the error
errorData - an object that provides additional
data about the error
requestParams - a reference to the Params
object that was passed to the call, and which includes the following
members:
context - a reference to the context object
that was passed to the original call
The callback
function should first check the value of the hadError field
for an indication of load success or failure.
Create a Params object for the fps.services.socialize.connect() method.
Our
example specifies the following members for the Params object ( connectParams)
that is passed to the fps.services.socialize.connect() method: provider - (Required) A string that specifies the provider
to whom to connect. The possible values for this parameter are:
“facebook”, “myspace”, and “twitter”.
callback - (Optional) A reference to a callback function.
The Social API calls the specified method with the results of the fps.services.socialize.connect() method
when it completes.
context - (Optional) An object that you create, which is
passed back unchanged to your application as one of the fields in
the Response object.
Call the fps.services.socialize.connect() method.
Call fps.services.socialize.connect(),
passing the Conf and Params objects as parameters. In the example,
these are the conf and connectParams parameters.
Define a callback function for fps.services.socialize.connect(). (Optional).
Recall
that we specified the callback function in the callback parameter: callback: onConnect.
Thus in our example the onConnect() function is
called when fps.services.socialize.connect() completes processing.
The
callback function specifies the following members of the Response
object: status - A string that specifies
the result code for the operation. “OK” indicates success; any other
string indicates failure.
statusMessage - A string that provides a
short textual description of the error for logging purposes.
operation - A string that contains the name
of the method that generated this response.
context - The context object that was passed
by the application as a parameter, or null if no context object
has been passed.
user - A User object with updated information
for the current user.
For more information on the User object,
see User Object
Notice how the onConnect(response) function
uses the Response object. It uses some of the common data members
such as response.status, response.statusMessage,
and response.operation, but it also uses the response.user data member,
which is specific to the connect() method. The response.user member
is a User object, and thus includes its members, nickname and photoURL,
which the example uses.
|