|
|
|
|
Server Side API (REST)From $1Table of contents
Contents
OverviewThe Gigya Socialize server-to-server API uses a REST-like interface. This means that the API method calls are made over the internet by sending HTTP GET or POST requests to the Socialize REST API server and the response is returned as XML. Nearly every software development environment provides methods for communicating over HTTP with a REST server. The REST API Reference provides specification to the various REST API methods supported by Gigya Socialize.
Prerequisites for Using REST API MethodsPlease make sure you comply with the following requirements prior to making a REST API method call:
Making an API CallA Sample API Call:The following is an example of an HTTP GET request: http://socialize.api.gigya.com/socialize.setStatus?uid=<User ID>&apiKey=<Your APIKey>&nonce=<nonce>×tamp=<current unix-time>& sig=<signature>&status=Hello The call is composed of the following elements:
Note: all parameter values should be in UTF-8 and URL-encoded.
Common Required ParametersThe following parameters are required for all API calls:
Making a Call Over HTTPSWhen making the API call over HTTPS, you may pass the secret parameter. In such case the timestamp, nonce and sig parameters are no longer required. Set the secret parameter with your Gigya Secret Key which can be obtained from the Site Setup page on the Gigya website. The following is an example of an HTTPS request with the secret parameter: https://socialize.api.gigya.com/socialize.setStatus?apiKey=<Your APIKey>&uid=<User ID>&secret=<Your Secret Key>&status=Hello
Note: Passing the secret parameter is allowed ONLY over HTTPS. If you attempt to pass the secret parameter over HTTP, the method will return the following error: "Secret Sent Over Http" (Error code: 403006).
Signing the RequestFor security reasons, Gigya requires every API call to be signed, so as to guarantee that it originated from an authorized partner and was not tampered with in transit.
Please Note: We use a different algorithm for signing REST API requests and for signing API calls on client applications (web pages or flash widgets). This section discusses signing REST API requests. To learn how to sign Gigya Socialize Client API methods (JavaScript & AS3) please refer to the Cryptographic Signatures section of the Developer's Guide.
The Authentication Flow
Generating the SignatureTo generate the signature, do the following:
You may use one of the OAuth libraries that can be found in http://oauth.net/code to build the Base String and generate the Signature.
Implementation Examples:
<?php
require_once("OAuth.php");
// Signature calculation method
// The parameters for the signature calculation are:
// 1. Your secret key
// 2. The HTTP method ('GET' or 'POST')
// 3. Method API name (e.g. 'setStatus')
// 4. The parameters of the API method. Note that each API method has a different set of parameter.
function calcSignatureREST($secretKey, $httpMethod, $apiMethod, $parameters) {
$req = OAuthRequest::from_request($httpMethod,'http://socialize.api.gigya.com/socialize.'.$apiMethod, $parameters);
$baseString = $req->get_signature_base_string();
return base64_encode(hash_hmac('sha1', $baseString, base64_decode($secretKey), true));
}
// Your secret key (can be found at the bottom of the Site Setup page on the Gigya website)
$secretKey = 'ENTER YOUR SECRET KEY HERE';
// The parameters for the socialize.getUserInfo API call:
$parameters = array(
"apiKey" => '2_OitqVv1ZZClsxml9-2L8eWZ-9FTTnTIu6S2-3jdEau4YuabkX4ssNcROopwy_rNv',
"nonce" => '128900583063345187',
"timestamp" => '1245584706',
"uid" => '_gid_+mtciUK98aqx57Dn+7yFhA==');
echo calcSignatureREST($secretKey, 'POST', 'getUserInfo', $parameters);
?>
Notes:
In order to make this code work in your environment, please:
class Post
#@Shmueli:
# I am using here the hmac/sha1 lib you can find here http://rubyforge.org/projects/ruby-hmac/
# user, id, status_message are instance methods of the Post class.
# this authentication method only works for the status api call as the http parameters are ordered
# hard coded and not sorted automatically.
# some modifications may be needed for other API calls.
def authenticate_and_share
#@ escape the status message and replace all + with %20 as spaces are CGI.escaped to +
message_text = CGI.escape(status_message).gsub('+', '%20')
user_id = user.id
#@ here are the parameters you need to supply from your Gigya site's settings page.
api_url = "http://socialize.api.gigya.com/socialize.setStatus"
api_key = "HERE_GOES_YOUR_API_KEY"
gigya_secret_key = "HERE_GOES_YOUR_GIGYA_SECRET_KEY"
#@ decode secret key and prepare nonce.
gigya_secret = Base64.decode64(gigya_secret_key)
timestamp = Time.now.gmtime.to_i
nonce = "#{user_id}#{id}#{timestamp}"
http_method = "GET" #@shmu: define your HTTP method
#@ parameters are ordered alphabetically, base string include HTTP method call and its parameters,
# all separated with unescaped "&"
parameters = CGI.escape("apiKey=#{CGI.escape(api_key)}&nonce=#{CGI.escape(nonce)}&status=#{message_text}
×tamp=#{timestamp}&uid=#{user_id}")
base_string = "#{http_method}&#{CGI.escape(api_url)}&#{parameters}"
#@ hmac/sha1 encription for the gigya secret and the base_string
hmacsha1 = HMAC::SHA1.digest(gigya_secret, base_string)
gigya_sign = Base64.encode64(hmacsha1).chomp.gsub(/\n/,'')
gigya_sign = CGI.escape(gigya_sign) #@shmu: we must escape the signature as well.
#@ finalized api request url with the signed signature
request_url = "#{api_url}?apiKey=#{api_key}&nonce=#{nonce}&status=#{message_text}×tamp=#{timestamp}
&uid=#{user_id}&sig=#{gigya_sign}"
puts request_url.inspect
#@ read the response
response_text = open(request_url).read
#@ handle error messages from gigya XML output.
regexp = /\<statusCode\>(.*?)\<\/statusCode\>/
status_code = response_text.scan(regexp).to_s.to_i
if status_code == 200
okmsg = "Gigya: Content Shared: #{status_message} [#{user.nick}]"
logger.info okmsg
return okmsg
else
raise "GIGYA RESPONSE ERROR: #{response_text.scan(/\<errorMessage\>(.*?)\<\/errorMessage\>/).to_s} \n\n
#{response_text.inspect} \n\n\n [id:#{id}, user:#{user}]\n\nStatusMessage: #{status_message}\n\n Basestring:
#{base_string}\n\n RequestURL: #{request_url}\n\n\n"
end
end
end
@The Ruby example code is written by Shmueli Ahdut. Notes:
In order to make this code work in your environment, please:
The following code example is a Perl script that executes the socialize.getUserInfo API call: #!/usr/local/bin/perl
use Digest::HMAC_SHA1 qw(hmac_sha1);
use MIME::Base64;
use URI::Escape;
my $secretKey = "[ENTER YOUR SECRET KEY HERE]";
my $apikey = "[ENTER YOUR API KEY HERE]";
my $uid = "[ENTER THE UID FOR A CONNECTED USER HERE]";
my $key = MIME::Base64::decode ($secretKey);
my $time = time ();
my $nonce = $time;
my $url = "http://socialize.api.gigya.com/socialize.getUserInfo";
my $method = "GET";
my $parms = "apiKey=" . $apikey;
$parms .= "&nonce=" . $nonce;
$parms .= "×tamp=" . $time;
$parms .= "&uid=" . uri_escape_utf8 ($uid);
my $sigbase = $method . "&" . uri_escape_utf8 ($url) . "&" . uri_escape_utf8 ($parms);
print STDERR "Base String: " . $sigbase . "\n\n";
my $hashed = hmac_sha1 ($sigbase, $key);
my $sig = MIME::Base64::encode ($hashed);
$sig =~ s/\n$//;
print STDERR "Sig: $sig\n";
my $url = "http://socialize.api.gigya.com/socialize.getUserInfo?apiKey=" . uri_escape ($apikey) . "&nonce=" . $time . "&sig=" . uri_escape ($sig) .
"×tamp=" . $time . "&uid=" . uri_escape ($uid);
print STDERR "URL: " . $url . "\n";
Notes:
In order to make this code work in your environment, please:
Receiving ResponsesAfter sending a REST request, you will receive a response, which is by default an XML string. Response XML Example:<?xml version="1.0" encoding="utf-8" ?>
<socialize.disconnectResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:com:gigya:api http://socialize.api.gigya.com/schema/1.0.xsd" xmlns="urn:com:gigya:api">
<statusCode>200</statusCode>
<statusReason>OK</statusReason>
</socialize.disconnectResponse>
The name of the root element in the response consists of the name of the request API method with the word "Response" appended to it, as demonstrated in the example above: All responses include two child elements, <statusCode> and <statusReason>, which comply with the HTTP status and status reason:
Some methods also return additional data as additional children of the root element. See the documentation of each method for details on the specific data it returns.
Note: if a response field contains no data, the return XML will not have a child element representing that field.
Error HandlingIf an error occurs, the response will include two additional child elements:
Response XML with Error Example:<?xml version="1.0" encoding="UTF-8" ?>
<socialize.setStatusResponse xmlns="urn:com:gigya:api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:com:gigya:api http://api.gigya.com/schema/1.0.xsd">
<statusCode>500</statusCode>
<statusReason>Internal Server Error</statusReason>
<errorCode>500024</errorCode>
<errorMessage>No valid session</errorMessage>
</socialize.setStatusResponse>
The system will return HTTP status "200 OK" for all application-level errors and return the detailed error in the body of the response. Please note that some errors, such as 503, or "Server Unreachable" will still be returned on the HTTP level and should be handled accordingly.
Error Codes:
< Back to 'Share Content' | Next to 'Advanced Customizations' >
Tags:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|