Making Direct Facebook API Calls Using REST

Gigya wraps the most popular social network APIs into a single API for your websites and mobile applications. As applications become more social, developers sometimes need to call a provider’s API directly in order to access information not exposed directly through our API. For example, the increased popularity of Facebook and the extent of that network’s API often leaves our developers wondering how they can use our API and the entire Facebook Graph API to achieve the best results. Below are two simple approaches that we recommend for doing this. NOTE: While the examples in this post use our PHP SDK, you are free to the server-side programming language of your choice. Gigya also provides SDK’s for Java and .NET. If your preferred language is something else and you write a wrapper to simplify access to our service, please consider sharing it with your peers in the community. Option #1 – socialize.getRawData The Gigya API offers the method socialize.getRawData() which enables retrieving raw data directly from Facebook, Myspace, Twitter, Yahoo and LinkedIn. 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. Here’s how we can query the Facebook User table by simply listing the fields we want:

[sourcecode language=”php”] // include PHP SDK (make sure the correct path is set) 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(); [/sourcecode]

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. Using FQL requires that the “Migrate to Facebook Graph API” option is checked in your Facebook Settings at the Gigya Dashboard. This setting instructs Gigya to use the new Facebook Graph API instead of the old Facebook Connect API. If you’re still using Gigya with Facebook Connect, we highly recommend you contact Gigya support before making the migration as this operation is irreversible. 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: [sourcecode language=”php”] //———————————- // STEP 1: DEFINE REQUIRED DATA //———————————- // include PHP SDK (make sure the correct path is set) 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]”; //———————————- // STEP 2: 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(); } //———————————- // STEP 3: 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(); [/sourcecode]
Option #2 – socialize.getSessionInfo For whatever reason, there may be times when you want to just skip going through Gigya and call the Facebook API directly. Facebook implements OAuth 2.0, so you’ll need an access token for all requests sent directly to them. Gigya provides a method called socialize.getSessionInfo() which will return a number of fields from the user’s session. One of these fields, authToken, is the access token you need for making a native Facebook Graph API call. Here’s an example:

[sourcecode language=”php”] // include PHP SDK (make sure the correct path is set) include_once “GSSDK.php”; // set your gigya application secret $gigya_apikey = “[INSERT YOUR API KEY]”; $gigya_secretkey = “[INSERT YOUR SECRET KEY]”; // set gigya uid $gigya_uid = “[INSERT GIGYA UID]”; /** * socialize.getSessionInfo * Returns the session info required for making direct API calls to facebook. * http://developers.gigya.com/display/GD/socialize.getSessionInfo+REST */ // define getSessionInfo request $request = new GSRequest($gigya_apikey,$gigya_secretkey,”socialize.getSessionInfo”); // set gigya uid and provider params $request->setParam(“uid”, $gigya_uid); $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(); } // get user session information $user_sessioninfo = $response->getData(); // set facebook access token $fb_accesstoken = $user_sessioninfo->getString(“authToken”,””); // call facebook api: graph.facebook.com/me $facebookdata = json_decode(file_get_contents( ‘https://graph.facebook.com’ . ‘/me’ . ‘?access_token=’ . $fb_accesstoken . ‘&fields=id,name,picture’ ),true); // output facebook data print “<pre>”; print_r($facebookdata); print “</pre>”; exit(); [/sourcecode] It’s important to remember that whether you’re using getRawData or getSessionInfo, your user must have an active session with the provider. In future posts, we’ll offer tips for making direct API calls to other providers (using both REST or javascript).

By Kevin White