You are not logged in.   Log in | Register

Web Pages (JavaScript)

From $1

This document provides the basic knowledge needed to start developing using Gigya Socialize. We will start with a basic example of a web page 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 HTML and JavaScript programming.

 

Basic Working Example

Run the Example:

The Example Code:  

<html>
<head>
    <title>Gigya Socialize Example</title>

    <!-- Step 1 - Including the socialize.js script: -->
    <script type="text/javascript" src="http://cdn.gigya.com/JS/socialize.js?apikey=INSERT-YOUR-APIKEY-HERE" ></script>

    <script type="text/javascript" >
   
    // Step 2 - Define a configuration object. Insert your Gigya APIKey below:
    var conf = { 
        APIKey: "INSERT-YOUR-APIKEY-HERE",
        enabledProviders: "facebook,myspace"
    };
   
    function Connect()
    {
        // Step 3 - Define parameters object:
        var params = {
           callback: onConnect,
           provider: "facebook"

        };   
   
        // Step 4 - Calling the Socialize API method - connect:
        gigya.services.socialize.connect(conf, params);
    }
   
    // Step 5 - Define callback function:
    function onConnect(response)
    {
        if (response.errorCode == 0)
        {
            // Update the page with the data received in the response:

            // inject the user's nickname to the "divUserName" div 
            document.getElementById('divUserName').innerHTML = response.user.nickname;
            // inject the user's photo to the image "src" attribute.
            document.getElementById('imgUserPhoto').src=response.user.photoURL;
        }
        else
        {
            //handle errors
            alert("An error has occurred!" + '\n' + 
                "Error details: " + response.errorMessage + '\n' +
                "In method: " + response.operation);
        }
    }   
   
  </script>
 
</head>
<body >
    <center>
    <input type="button" onclick="Connect()" value="Connect to facebook"   />
    <div id="UserInfo">
        <div id="divUserName"></div>
        <div id="divUserPhoto"><img id="imgUserPhoto" src="http://www.gigya.com/wildfire/i/transparent.GIF" onerror="this.src='http://www.gigya.com/wildfire/i/transparent.GIF'" /></div>
    </div>
    </center>
</body>
</html>
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 6 and 12, 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 5 basic steps of using the Gigya Socialize API:

Step 1: Include the Socialize JS file in your pages

Including the Socialize JS file in your pages is a prerequisite for using Gigya Socialize API method calls.

Every page that uses the Gigya Socialize API should include the Socialize Java-script library (only once) in the <HEAD> section:

<script type="text/javascript" src="http://cdn.gigya.com/JS/socialize.js?apikey=INSERT-YOUR-APIKEY-HERE" ></script>
Notes:
  • Please, replace "INSERT-YOUR-APIKEY-HERE" in the code, to your Gigya API-Key, which can be obtained on the Site Setup page on Gigya website.
  • If you are implementing Socialize in an https page, please read about using Socialize in secure pages.

 

Step 2: Create a global configuration object

Every GS API method requires a configuration object as the 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.

var conf = { 
       APIKey: "INSERT-YOUR-APIKEY-HERE",
       enabledProviders: "facebook,myspace"
   };
Note:  Please, replace "INSERT-YOUR-APIKEY-HERE" in the code, to your Gigya API-Key, which 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 full specifications).

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

Step 3: Create a method specific params object

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.

var params = {
    callback: onConnect,
    provider: 'facebook'
}

 

Each method in the Gigya Socialize API accepts different 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.
    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 4), 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 params' object in the above example conforms to this definition, and thus can be passed as the second parameter of the connect method.

 

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 = {
     callback: onConnect,
     provider: "facebook"
};      
gigya.services.socialize.connect(conf, paramsConnect);

var paramsGetFriendsInfo = {
     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 4: Execute the API Method

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

gigya.services.socialize.connect(conf, params);

 

Step 5: 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 parameter "callback": onConnect. Thus in our example the onConnect function is called when the processing of the method socialize.connect completes:

function onConnect(response)
    {
        if (response.errorCode == 0)
        {
            // Update the page with the data received in the response:

            // inject the user's nickname to the "divUserName" div 
            document.getElementById('divUserName').innerHTML = response.user.nickname;
            // inject the user's photo to the image "src" attribute.
            document.getElementById('imgUserPhoto').src=response.user.photoURL;
        }
        else
        {
            //handle errors
            alert("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)