Direct API Calls to Providers

Skip to end of metadata
Go to start of metadata

 

There are times when making direct calls to a provider’s API is necessary when Gigya’s API doesn’t support a particular method or function. For example, you might need to:

  • Make a Facebook Graph API call to retrieve a user’s likes, groups, events or videos
  • Call the Twitter API directly to get a user's timeline
  • Call the Linked-In API to retrieve a person's education history
  • Call the Messenger Connect API to get the user's activity stream

With Gigya, making direct calls to provider APIs is simple and straightforward. Here is a quick overview of two options you can implement to accomplish this.

Option #1 - Native API Call Using OAuth

Since most providers implement some version of OAuth, making a native API call on behalf of a user requires a valid access token (sometimes known as a "session token"). Gigya provides both Javascript and REST versions of a method called socialize.getSessionInfo() which returns a field called “authToken”. This is the access token you’ll need for making API calls to platforms like Facebook, Twitter, etc. Here’s one example of calling the Facebook graph api using our PHP SDK and REST:

// include Gigya PHP SDK
include_once "GSSDK.php";

 // set gigya variables
$gigya_apikey = "[INSERT YOUR API KEY]";
$gigya_secretkey = "[INSERT YOUR SECRET KEY]";
$gigya_uid = "[INSERT GIGYA UID]";

// format request: socialize.getSessionInfo
$request = new GSRequest($gigya_apikey,$gigya_secretkey,"socialize.getSessionInfo");
$request->setParam("uid", $gigya_uid);

// set the provider (facebook in this case)
$request->setParam("provider", "facebook");

// send to gigya
$response = $request->send();

// handle any errors
if ($response->getErrorCode()!=0) {
    echo ("Got error on getSessionInfo: " . $response->getErrorMessage());
    exit();
}

// retrieve the access token
$accesstoken = $response->getString("authToken","");

// call facebook api: graph.facebook.com/me
$facebookdata = json_decode(file_get_contents(
 'https://graph.facebook.com'
 . '/me'
 . '?access_token=' . $accesstoken
 . '&fields=id,name,picture'
 ),true);

// output facebook data
print "<pre>";
print_r($facebookdata);
print "</pre>";
exit();

socialize.getSessionInfo() is supported by providers such as Facebook, Twitter, Yahoo, LinkedIn, etc. Each provider has their own policy for expiring access tokens so make sure you implement some conditional processing in case the access token is no longer valid. If this happens, you’ll need to login the user in order to establish a new active connection with a valid access token.

Option #2 – Use Gigya's getRawData Method

The Gigya API offers the method socialize.getRawData which enables retrieving raw data directly from some providers. The response data is returned exactly as it is received from the provider without translation by Gigya, so it’s the developer’s responsibility to know how to use the information.

 

Update: Facebook does not support FQL requests for apps created after August 2014. If you have a newer app, instead of using socialize.getRawData with the 'fields' parameter to send an FQL request, you should use the 'path' parameter or use the socialize.facebookGraphOperation method.

 

Here’s how we can query the Facebook User Table by simply listing the fields we want:

// include PHP SDK
include_once "GSSDK.php";

// set your gigya application keys
$gigya_apikey = "[INSERT YOUR API KEY]";
$gigya_secretkey = "[INSERT YOUR SECRET KEY]";

// set gigya userid
$gigya_uid = "[INSERT GIGYA UID]";

// define getRawData request
$request = new GSRequest($gigya_apikey,$gigya_secretkey,"socialize.getRawData");

// set gigya uid and provider params
$request->setParam("uid", $gigya_uid);
$request->setParam("provider", "facebook");

// set the facebook fields we want
$request->setParam("fields","uid,name,pic");

// send request to gigya
$response = $request->send();

// handle any errors
if ($response->getErrorCode()!=0) {
	echo ("Got error on getRawData: " . $response->getErrorMessage());
	exit();
}

// convert facebook json data to array for convenience
$facebookdata = json_decode($response->getData()->getString("data",""), true);

// output facebook data
print "<pre>";
print_r($facebookdata);
print "</pre>";
exit();

By default, the socialize.getRawData method retrieves data fields related to the specified user. However, it can also be used for executing any Facebook FQL query which gives you a SQL-style interface for querying data. 

For now, here’s a simple FQL call using socialize.getRawData. Notice that we first obtain the user’s Facebook uid by calling Gigya’s socialize.getUserInfo method and looping over the user’s identities. This ensures the user has an established connection with Facebook. Because this user data is usually in cache, calling getUserInfo before getRawData will have negligible performance penalties. We encourage you to follow Facebook’s suggested performance guide when making FQL requests:

// include PHP SDK
include_once "GSSDK.php";

// set your gigya application keys
$gigya_apikey = "[INSERT YOUR API KEY]";
$gigya_secretkey = "[INSERT YOUR SECRET KEY]";

// set user ids
$gigya_uid = "[INSERT GIGYA UID]";

//----------------------------------
// GET FACEBOOK UID
//----------------------------------

// define getUserInfo request
$request = new GSRequest($gigya_apikey,$gigya_secretkey,"socialize.getUserInfo");

// set gigya uid
$request->setParam("uid", $gigya_uid);

// send to gigya
$response = $request->send();

// handle any errors
if ($response->getErrorCode()!=0) {
 echo ("Got error on getUserInfo: " . $response->getErrorMessage());
 exit();
}

// set blank facebook uid
$facebook_uid = "";

// get user identities
$user_identities = $response->getData()->getArray("identities");

// loop over and get facebook uid
if ($user_identities != null) {
     foreach ($user_identities as $identity) {
          if ($identity->getString("provider",null) == "facebook"){
              $facebook_uid = $identity->getString("providerUID");
          }
     }
}

// error: user is not connected with facebook
if ($facebook_uid == "") {
 echo 'error: you must first connect with facebook';
 exit();
}

//----------------------------------
// RUN FACEBOOK FQL QUERY
//----------------------------------

// define getRawData request
$request = new GSRequest($gigya_apikey,$gigya_secretkey,"socialize.getRawData");

// set gigya uid and provider params
$request->setParam("uid", $gigya_uid);
$request->setParam("provider", "facebook");

// set facebook fql
$request->setParam(
 "fields",
 "SELECT uid, name, pic FROM user WHERE uid = '$facebook_uid '"
);

// send request
$response = $request->send();

// handle any errors
if ($response->getErrorCode()!=0) {
 echo ("Got error on getRawData: " . $response->getErrorMessage());
 exit();
}

// convert facebook json data to array for convenience
$facebookdata = json_decode($response->getData()->getString("data",""), true);

// output facebook data
print "<pre>";
print_r($facebookdata);
print "</pre>";
exit();

It’s important to remember that whether you’re using getRawData or getSessionInfo, your user must have an active session with the provider.

 

Related Links