Table of contents
- 1. Basic Concepts
- 1.1. An Asynchronous Programming Model
- 1.1.1. Definitions:
- 1.2. The API namespace
- 1.1. An Asynchronous Programming Model
- 2. Getting Started
- 3. What's Next?
Welcome to the developer's documentation for Gigya ActionScript API.
The Developer's Guide is a practical tutorial-like guide for widget developers who wish to incorporate Gigya ActionScript API in their widget.
Basic Concepts
An Asynchronous Programming Model
Gigya Socialize uses an asynchronous programming model in which operations are triggered and then run in the background until they are completed.
Upon successful or unsuccessful completion, the operation invokes a callback function - which is provided by the developer, and returns a response object that includes the results of the operation. The callback function should handle the response in an appropriate manner, as determined by the application.
The same callback function may be used to handle multiple events.
Definitions:
Callback function - Callback functions are used to return results of Gigya ActionScript API function calls when the method is expected to take a long time to execute. This programming model is known as asynchronous because the result of a function call is not returned when the function call returns but instead it is returned after a while by calling the associated callback function.
Context object - Context objects are commonly used in asynchronous programming to allow the application to store and pass information between callbacks. All the Gigya ActionScript API method calls can accept a context object and then pass it back to the application as a parameter of the callback function. Gigya ActionScript API never attempts to read the context object and it is left to the application to create and interpret its meaning.
The API namespace
The Gigya API uses a 3 level hierarchical namespace, for specifying method names, with the scheme:
gigya.services.{service name}.{method name}
The only exception to this rule is the gigya.load() method, which is described below.
Getting Started
Using Gigya API is a four step process:
Step 1: Include the gigya.swc file in your project
Download the gigya.swc file appropriate for your development environment from the Gigya website: Unzip the file and include it in your project:
The gigya.swc file provides boot strapping code for the Gigya API and should never be changed. We encourage you to get the latest version of this file whenever you make changes to your project so that it would include the latest improvements and fixes.
Note:
The gigya.swc will implicitly give the Gigya API code permissions to access your SWF's data members.
If the Gigya API is used in a SWF which is loaded by another SWF, or within an hierarchy of other SWFs, then all the parent SWFs must give Gigya permissions to access them as well. Implement that by adding the following lines of code:
System.security.allowDomain("cdn.gigya.com");
System.security.allowInsecureDomain("cdn.gigya.com") Security.allowDomain("cdn.gigya.com");
Security.allowInsecureDomain("cdn.gigya.com");
Step 2: Create a global configuration object
Every Gigya API method requires a configuration object as its first parameter.
This object is used for storing global configuration parameters, which are common to all the API calls and are expected to remain static for the lifetime of the application.
The configuration object can be created once and passed to all the method calls in the lifetime of the application.
Currently the object's members are:
- pid - your Gigya partner-ID. A Gigya partner-ID can be obtained from your Account Settings page on the Gigya website.
- mcRoot - the _root of the flash container.
Best practice is, to define one global 'conf' object and use it throughout the application:
// Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var loadConf:Object = {
mcRoot: _root,
pid: "Put Your partner-ID here"
} // Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var conf:Object = {
mcRoot: root,
pid: "Put Your partner-ID here"
} // Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var conf:Object = {
pid: "Put Your partner-ID here"
} 1. Modify "Put Your partner-ID here" in the code, to your Gigya partner-ID.
2. In Flex mcRoot parameter can not be initialized statically, since the value this.root can not be accessed statically. Instead it should be initialized inside the init() method:
// Initialize the "mcRoot" attribute of the conf object - should refer to the root of the flash container this.conf.mcRoot=this.root;
Step 3: Load the required service/s
Prior to using Gigya API methods the corresponding services must be loaded. Loading services is done by executing the gigya.load(conf, params) method and waiting for your callback method to be called with an indication if the load operation being successful or not.
Step 3.1: Create a load's params object
Every Gigya API method requires a "params" object as its second parameter. The params object enfolds all the specific parameters sent to the API method.
The method gigya.load expects the following members in the params object:
| Required | Name | Type | Description |
| Required | services | string | A comma separated list of service names that you plan on using in your widget. |
| Optional | callback | function | A reference to a callback function. Gigya will call the specified function along with the results of the API method when the API method completes. |
| context | object | A developer-created object that is passed back unchanged to the application as one of the fields in the response object. |
In other words: the gigya.load method requires a parameter called 'services', in addition to the optional parameters 'callback' and 'context'.
The 'var loadParams' object in the following example conforms to this definition, and thus can be passed as the second parameter of the gigya.load method:
// 3.1 Create the load-parameters object
var loadParams:Object = {
services: 'social,reports',
callback: onGigyaInit
}
Step 3.2: Execute the gigya.load Method
Call the API method gigya.load. Transfer the 'conf', and 'params' objects as parameters:
// 3.2 Load the services gigya.load(conf,loadParams);
Step 3.3: Define a Callback Function for the Load Operation
The API uses an asynchronous programming model, meaning - that gigya.load will run in the background and call a developer provided callback function when it completes.
Recall that we specified the callback function in the "callback" attribute of the params object - callback:onGigyaInit. Thus in our example the onGigyaInit function is called when the processing of the method gigya.load completes:
// 3.3 Wait for the load to complete and handle failure\success
function onGigyaInit(response:Object): void
{
if (res.hadError) {
// handle failure
trace('An error has occurred while attemting to load Socilaize service');
} else {
// successful load
//continue knowing that the required services are now available.
//please note that some services may require initialization after they are loaded.
}
}
The callback function is a developer-defined function. The only constraint is on its signature - Gigya 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 - display an error message to the end-user if an error has occurred.
The response object for the gigya.load operation include the following members:
- hadError - a boolean indicating if an error has occurred.
- errorCode - an error code or 0 if no error has occurred.
- errorMessage - a string containing the description of the error.
- errorData - this object provides additional data about the error. It includes the following members:
- failedServices - a comma separated list of services that failed to load.
- detailedErrors - a string with additional details about the errors causing each failure respectively.
- requestParams - a reference to the "params" object that was passed to the original call. This object includes the following members:
- services
- callback - a reference to the callback function.
- context - a reference to the "context" object that was passed to the original call.
The callback function should initially check the value of hadError field as indication for load failure\success.
Step 4: Making API calls
Step 4.1: Create a method specific params object
As mentioned above, every Gigya API method requires a "params" object as the second parameter. The params object enfolds all the specific parameters sent to the API method. Each method in the Gigya API accepts different set of parameters, as appropriate for that method. Additionally, all the methods in the Gigya API accept the following common parameters:
- callback - A reference to a callback function. Gigya will call the specified function along with the results of the API method when the API method completes.
- context - A developer-created object that will be passed back unchanged to the application as one of the fields in the result object.
The API Reference, specifies the list of parameters ("params" object members) which each API method accepts - some are required and some are optional.
var paramsGetGeo:Object = {
callback: getGeoResponse,
cid: '123'
};
gigya.services.env.getGeo(conf,paramsGetGeo);
var paramsGetFriends:Object = {
callback: getFriendsResponse
};
gigya.services.social.getFriends(conf, paramsGetFriends); Another option is to define the params object inline. For example:gigya.services.env.getGeo(conf, {
callback: getGeoResponse,
cid: '123'
});
gigya.services.social.getFriends(conf, {
callback: getFriendsResponse
});
Step 4.2: Execute the API Method
All the methods in the Gigya API (except the gigya.load method) have a common signature:
gigya.services.service_name.method_name(conf,params);
Both "conf" and "params" are actually objects that contain multiple members used for configuring the method. We choose to use these two objects instead of passing paremeters in the classical way to give us flexibility in adding new parameters without breaking older code.
The API uses an asynchronous programming model where operations are started by calling and API method, run in the background and call a user supplied callback function when they are complete. The callback function is called regardless if the operation succeeded of failed, so the application can handle both cases gracefully. A single callback function may be used to handle multiple events, in this case the application can set a context object when it calls the API and the callback function can later use this object to determine the context in which it was called and react accordingly.
Step 4.3: Define a Callback function (optional)
When an API call is made it returns immediately and starts processing the request in the background. When the processing is complete the user provided callback function is called with the purpose of passing the API method's return values. The callback function is a developer-defined function. The only constraint is on its signature - Gigya expects the callback function to receive one parameter, which is the response object.
The response object contains the values returned from the specific API method. The callback function should analyze the response object and handle it appropriately. For example - display an error message to the end-user if an error has occurred. In particular, the callback function should verify that the status field of the response object is "OK", as any other value indicates an error.
Every response object contains common members (values which are returned by all API methods).
The common members of the response object are specified in the following table:
| Field | Type | Description |
| hadError | Boolean | Indicates if an error has occurred |
| requestParams | Object | This is the params object that was passed to the original method call. |
| errorCode | int | An error code or 0 if no error has occurred. |
| errorMessage | String | The description of the error.T |
| errorData | Object | This object provides additional data about the error. |
Each API method defines its additional members in the response object. Method specific members are detailed in the reference section for each API call.
For example, in the following code we define a callback method for the social.getFriends API method:
// Step 4.3 - Define callback function:
function getFriendsResponse(response: Object): void {
if (response.hadError)
{
// handle errors
trace(response.errorMessage);
} else {
//Use the friends data
var friends:Array = response.friends.asArray();
}
}
Notice how we used the response object in the implementation of getFriendsResponse(response) function:
We used some of the common data members: response.hadError, response.errorMessage.
We also used the response.friends data member, which is specific to the getFriends method.
What's Next?
You should now be ready to start using the basic features of Gigya ActionScript API.
You may consult the API Reference section - for full specifications of the Gigya Socialize API
< Back to 'Gigya ActionScript API' | Next to 'API Reference' >



Comments