You are not logged in.   Log in | Register

Flash (ActionScript3)

From $1

This document provides the basic knowledge needed to start developing using Gigya Socialize. We will start with a basic example of a widget that uses Gigya Socialize - the Gigya Socialize "Hello World", and a guide for using Gigya Socialize API methods. We highly recommend reading this document before starting to code. 

This document is designed for people familiar with ActionScript3 programming.

 

 

 

 

Basic Working Example

Note: the following example is built using the Flash CS3 IDE. If you are using an alternative development environments, you will still find this example useful, since the core code is identical.

 

Run the Example:

The Example Source (FLA)

You may download the source of the running example here. The zip file includes the fla file of the above example and the gigya.swc file which must be included in the project (this issue is further explained below).

The Example Code: 

We have created an AS3 FLA file and placed three UI components:

  • Button named 'connectBtn'.
  • Label named 'label'.
  • UILoader named 'image'.

The following is the code entered in the main document Actions-Frame:

connectBtn.enabled = false;
label.text = 'Loading Socilaize...';
connectBtn.addEventListener(MouseEvent.CLICK, onConnectBtnClick);

// Step 2 - Define a configuration object. Insert your Gigya APIKey below:
var conf:Object = { 
    APIKey: 'INSERT-YOUR-APIKEY-HERE',
    mcRoot: root,
    enabledProviders: '*'
}

// Step 3 - Load socialize service
// 3.1 Create the load-parameters object
var loadParams:Object = {
    services:'socialize',
    callback:onGigyaInit
}

// 3.1 Load the services
gigya.load(conf,loadParams);

// 3.2 Wait for the load to complete and handle failures\success
function onGigyaInit(response): void
{
    if (response.hadError) {
        // handle failure
	label.text = 'An error has occurred while attempting to load Socialize service';
    } else {  
        // successful load
	label.text = '';
	connectBtn.enabled = true;    
    }
}


function onConnectBtnClick(event: MouseEvent): void {
    // Step 4 - Define parameters object:
    var connectParams:Object = {
        callback: onConnect,
        provider: 'facebook'
    };   
   
     // Step 5 - Calling the Socialize API method - connect:
     gigya.services.socialize.connect(conf, connectParams);
}

// Step 6 - Define callback function: 
function onConnect(response: Object): void {
    if (response.errorCode == 0)
    {
        // inject the user's nickname to the label 
        label.text = response.user.nickname;
        // inject the user's photo to the image "source" attribute.
        image.source=response.user.photoURL;
    } else {
        //handle errors
        label.text = "An error has occurred!" + '\n' + 
                "Error details: " + response.errorMessage+ '\n' +
                "In method: " + response.operation;
    }
}
Note:
In order to make this code work in your environment, please:
  • Click the "Copy sample code" (link located above code) - This will open a popup window with the text version of the code, which you may copy.
  • Replace "INSERT-YOUR-APIKEY-HERE" string in lines 7, to your Gigya API-Key, which can be obtained on the Site Setup page on Gigya website. See also the Socialize Setup documentation.
  • For development and testing purposes, it is possible to run the example on 'localhost' and with any valid API key.
  • When deploying, make sure your page loads from a web server that uses a DNS name (e.g. www.yoursite.com). If required, you may use your "hosts" file to map a DNS name that will point to the web server on your local machine.

 

 

The Example demonstrates the 6 basic steps of using the Gigya Socialize API:

Step 1: Include the gigya.swc file in your project

Download the gigya.swc file from the Gigya website. Unzip the file and include it in your project by placing the file in your working directory and performing the following steps in your Flash project:

Click "File" > "Publish Settings..." > select the "Flash" tab > click the "Setting..." button > select the "Library path" tab > click the "Browse to SWC file" button (red icon) > browse and select gigya.swc file > click 'OK' on both windows.

Flash-gigya.swc.gif 

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:

Security.allowDomain("cdn.gigya.com");
Security.allowInsecureDomain("cdn.gigya.com");

 

Step 2: Create a global configuration object

Every GS 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 conf object can be created once and passed to all the method calls in the lifetime of the application. Best practice is, to define one global conf object and use it throughout the application:

// Step 2 - Define a configuration object. Insert your Gigya APIKey below:
var conf:Object = { 
    APIKey: 'Put Your APIKey here',
    mcRoot: root,
    enabledProviders: 'facebook,myspace'
}
Note:  Modify "Put Your APIKey here" in the code, to your Gigya API-Key. A Gigya API-Key can be obtained on the Site Setup page on Gigya website.

The conf object consists of various members (please refer to the API Reference for the full specification).

In the Example above, we have used three of the conf object members: 
Gigya_Socialize_API_2.0/API_reference/Objects/Conf_object#

  • APIKey - The API key provided by Gigya.The APIKey is a required configuration parameter.
  • mcRoot - This parameter should refer to the root of your flash container. The mcRoot is a required configuration parameter.
  • enabledProviders - A comma-separated list of providers which will be enabled in Gigya Socialize. The enableProviders is an optional configuration parameter. You may use '*' to enable all the providers.

Gigya_Socialize_API_2.0/API_reference/Objects/Conf_object#Edit section

Step 3: Load Socialize service

The Socialize service must be loaded prior to using the Socialize API methods. Loading the Socialize service 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.

Note: You should not call any API method before your load callback method is called and an indication of successful load is received.

 

Step 3.1: Create a load's params object

Every GS 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. For the usage of Socialize service, this parameter should be set with the string 'socialize'.
(To learn about additional services provided by Gigya, please refer to the Additional ActionScript APIs documentation).
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 will be 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 our example conforms to this definition, and thus can be passed as the second parameter of the gigya.load method, as shown below.

// 3.1 Create the load-parameters object
var loadParams:Object = {
    services:'socialize',
    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
        label.text = 'An error has occurred while attemting to load Socilaize service';
    } else {  
        // successful load
        label.text = '';
        connectBtn.enabled = true;    
    }
}

The callback function is a developer-defined function. The only constraint is on its signature - Gigya Socialize 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: Create a method specific params object

As mentioned above, every GS API method requires a "params" object as the second parameter. The params object enfolds all the specific parameters sent to the API method.

// Step 4 - Define parameters object:
var connectParams:Object = {
    callback: onConnect,
    provider: 'facebook'
};

 

Each method in the Gigya Socialize API accepts different set of parameters, as appropriate for that method. Additionally, all the methods in the Gigya Socialize 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.
    The callback function should be defined with the following signature: functionName(response)
    The "Response object Data Members" table below provides specification of the data which is passed to the callback function.
  • context - A developer-created object that will be passed back unchanged to the application as one of the fields in the response object.

 

The API Reference, specifies the list of parameters ("params" object members) which each API method accepts - some are required and some are optional.

In our Example we used the method socialize.connect (in Step 5), which defines the following parameters: 

 

Required Name Type Description
Required provider string The provider to connect to. The optional values for this parameter are: 'facebook', 'myspace', 'twitter', 'yahoo', 'linkedin'.
Optional useFacebookConnect boolean Assigning "true" to this parameter, will cause Gigya Socialize to pop-up Facebook Connect UI when connecting to the user's Facebook account (i.e. when the user presses the Facebook button of the Connect UI). You may find more information about the subject in Using Facebook Connect section in the Developer's Guide.
Note: this parameter is not supported in ActionScript environment.
  invite Invite object If this parameter is specified then the "Invite" dialog will be displayed when a user connects to Facebook. You may find more information about the subject in Inviting Friends section in the Developer's Guide.
  shortURLs string This parameter is relevant only if you use the 'invite' parameter defined above.
Using this parameter you may determine whether to use Gigya's URL shortening service for URLs, which are published through this method. The optional values for this parameter are:
  • 'always' (default): always shorten URLs.
  • 'whenRequired': shorten URLs when needed - when posting to Twitter, LinkedIn, MySpace and Yahoo where the status update is limited to 140 char.
  • 'never' - never shorten URLs.

When Gigya's URL shortening service is active, Gigya tracks all the traffic coming from the distributed URLs. In such case, 'Referred Traffic' reports will be available to you.


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.
The callback function should be defined with the following signature: functionName(response)
The "Response object Data Members" table below provides specification of the data which is passed to the callback function.
  cid string A string of maximum 100 characters length. This string will be associated with each transaction and will later appear on reports generated by Gigya, in the "Context ID" combo box. The cid allows you to associate the report information with your own internal data, for example, to identify a specific widget or page on your site/application. The "Context ID" combo box lets you filter the report data by site/application context.
Note: the value of this parameter overrides the value of the identical parameter in the Conf object.
  context object A developer-created object that will be passed back unchanged to the application as one of the fields in the response object.

This means: that the method requires a parameter called 'provider', in addition to some optional parameters. 

The 'var connectParams' object in the above example conforms to this definition, and thus can be passed as the second parameter of the connect method, as shown below.

 

Note:

Some of the parameters may appear both in the Conf object and in the method's specific "Params" object. An example for that is the cid parameter. In such case the value from the Params object overrides the value from the Conf object.
For example:

var conf = { ...,  cid: "abc" }
var params = { ...,  cid: "xyz" }
gigya.services.socialize.connect(conf,params);  // The cid "xyz" will be used. 

This can be useful, for defining global default in the Conf object, and overriding the default in some methods, by setting a different value in the method's Params object.

 

Best practice

It is important to define an independent params object per method call, as opposed to editing and reusing the same object for several method calls. This is due to the fact that the methods are running asynchronously. Best practice is to concatenate the method name to the params object name. For example:

var paramsConnect:Object = {
     callback: onConnect,
     provider: 'facebook'
};      
gigya.services.socialize.connect(conf, paramsConnect);

var paramsGetFriendsInfo:Object = {
     detailLevel: 'extended',
     callback: getFriendsResponse
};      
gigya.services.socialize.getFriendsInfo(conf, paramsGetFriendsInfo);
Another option is to define the params object inline. For example:
gigya.services.socialize.connect(conf, {
           callback: onConnect,
           provider: 'facebook'
        });

gigya.services.socialize.getFriendsInfo(conf, {
     detailLevel: 'extended',
     callback: getFriendsResponse
    });

 

Step 5: Execute the API Method

Call the API method socialize.connect. Transfer the 'conf', and 'connectParams' objects as parameters.

// Step 5 - Calling the Socialize API method - connect:
gigya.services.socialize.connect(conf, connectParams);

 

Step 6: Define a Callback function (optional)

The API uses an asynchronous programming model, meaning - that socialize.connect 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:onConnect. Thus in our example the onConnect function is called when the processing of the method socialize.connect completes:

// Step 6 - Define callback function: 
function onConnect(response: Object): void {
    if (response.errorCode == 0)
    {
        // inject the user's nickname to the label 
        label.text = response.user.nickname;
        // inject the user's photo to the image "source" attribute.
        image.source=response.user.photoURL;
    } else {
        //handle errors
        label.text = "An error has occurred!" + '\n' + 
                "Error details: " + response.errorMessage + '\n' +
                "In method: " + response.operation;
    }
}

 

The callback function is a developer-defined function. The only constraint is on its signature - Gigya Socialize 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. In particular, the callback function should verify that the errorCode field of the response object equals zero (response.errorCode == 0), 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
errorCode integer The result code of the operation. Code '0' indicates success, any other number indicates failure. For a complete list of error codes, see the Error Codes table.
errorMessage string A short textual description of an error, associated with the errorCode, for logging purposes.
operation string The name of the API method that generated this response.
context object The context object passed by the application as parameter to the API method, or null if no context object has been passed.

 

Each API method defines its additional members in the response object. Method specific members are detailed in the reference section for each API call.

In our Example - the socialize.connect method defines the following additional member:

user              User object User object with updated information for the current user.

Note - The 'user' parameter is of the type 'User object'. Please refer to the specification of User object in the API reference section.

 

Notice how we used the response object in the implementation of onConnect(response) function:

We used some of the common data members: response.errorCode, response.errorMessage, response.operation

We also used the response.user data member, which is specific to the connect method. response.user is of the type User object, and thus includes the members - nickname and photoURL, which are used in the example: response.user.nickname, response.user.photoURL

 

What's Next?

You should now be ready to start using the basic features of Gigya Socialize API.

  • You may now want to return to the Gigya website - Getting Started Step 2 - Choose your Implementation Type. This will guide you further to different implementation scenarios.
  • You may wish to continue reading the Development Guide for further learning about:
  • You may consult the API Reference section - for full specifications of the Gigya Socialize API

 

 

 < Back to 'Developer's Guide' root | Next to 'Using Socialize Widgets' >

Tags:
Files (0)