Sunday, February 13, 2011

Calling OOyala player API using C#.

Hello Friends,

Some or any of you might have used OOyala tool for you projects as its one of the best video management tools i have ever seen.

Some days back i have given a task to get ooyala integrated to our project and i decided to do it via their API but all the samples given on site are eighter in PHP or in Ruby but i want things to be done in c# so by following their documentation i have finally got the response from ooyala site.

Here i am showing you two functions and two properties.

1. _params: i have used sorted dictionary because ooyala demands that all parameters should be in acceding order.

2. Query type: specify which API should be called.

3. send_request(): It does the required encoding of parameters by using the secret and partner code and generated the URL by which XML response can be fetched.

4. getXMLResponse(): Get all XML from URL.

#region Properties

        private string PARTNET_CODE = "PARTNET_CODE";
        private string PARTNET_SECRET = "PARTNET_SECRET";


        private string _queryType = string.Empty;
        public string QueryType
        {
            get
            {
                return _queryType;
            }

            set
            {
                _queryType = value;
            }

        }

        private SortedDictionary<string, string> _params;
        public SortedDictionary<string, string> Params
        {

            get
            {
                return _params;
            }
            set
            {
                _params = value;
            }

        }

        #endregion



        #region Public function



        public string getXMLResponse()
        {


            string sUrl = send_request();
            StringBuilder oBuilder = new StringBuilder();
            StringWriter oStringWriter = new StringWriter(oBuilder);
            XmlTextReader oXmlReader = new XmlTextReader(sUrl);
            XmlTextWriter oXmlWriter = new XmlTextWriter(oStringWriter);
            while (oXmlReader.Read())
            {
                oXmlWriter.WriteNode(oXmlReader, true);
            }
            oXmlReader.Close();
            oXmlWriter.Close(); 

            return oBuilder.ToString();


        }


        /// <summary>
        /// Returns the URL for request parameters 
        /// </summary>
        private string send_request()
        {

            string url = "http://api.ooyala.com/partner/" + QueryType + "?pcode=" + PARTNET_CODE;
            foreach (KeyValuePair<string, string> pair in _params)
            {
                PARTNET_SECRET += pair.Key + "=" + pair.Value;
                url += "&" + HttpUtility.UrlEncodeUnicode(pair.Key) + "=" + HttpUtility.UrlEncodeUnicode(pair.Value);
            }
            SHA256Managed sh = new SHA256Managed();
            byte[] request = System.Text.Encoding.ASCII.GetBytes(PARTNET_SECRET);
            sh.Initialize();
            byte[] b4bbuff = sh.ComputeHash(request, 0, request.Length);
            string hashString = System.Text.Encoding.ASCII.GetString(b4bbuff);

            string encryptedRequest = HttpUtility.UrlEncode((Convert.ToBase64String(b4bbuff)).Substring(0, 43));
            url += "&signature=" + encryptedRequest;

            return url;
        }
#endregion


Once you get the XMl response do what ever you want, Deserilise it and use it in c# code or parse it with jQuery choice is yours.

Hope it helps.

Happy coding.

Regards,
U

14 comments:

bclee_usa said...

hi:
Thanks for posting your code. I'm getting an "invalid signature" error. Where you able to generate a valid signature using your code as posted. Thanks again!!!

Usman Shabbir said...

Thanks Bclee, if you need any other help let me know.

Er.Anil Kumar said...

HI I ma getting the same error "Invalid Signature"

Usman Shabbir said...

Hi Anil,

Have you tired the exact code with your partner code ?

Er.Anil Kumar said...

yes,I am using the right Partner & Secret code.Because I have copied it directly from Ooyala Account.

Is this issue may be with expires duration?

Usman Shabbir said...

Can u share the prams list that you are passing to this class ?

Er.Anil Kumar said...
This comment has been removed by the author.
Er.Anil Kumar said...

http://www.ooyala.com/partner/labels?pcode=&expires=&signature=

I am passing pcode and expires

If I take expires=1314370579 signature is conatant in each request
and if I take it dynamic signature keep on changing.
In both case(static or dynamic expires values)it is not working

Usman Shabbir said...

Please use this function for time stamp creation


private DateTime UnixEpoch = new DateTime(1970, 1, 1);
public int GetUnixTimestamp(DateTime dt)
{
TimeSpan span = dt - UnixEpoch;
return (int)span.TotalSeconds;
}

Er.Anil Kumar said...

Thanks
Issue resolved I was not passing mode parameter

coder said...

Thanks for the code but I am getting "The remote server returned an error: (400) Bad Request." error

coder said...

I have provided pcode, expires, embedcode and signature in the URL.I am not getting why i am getting this error.

Usman Shabbir said...

Please share the sample code where you are creating signature.

coder said...

string url = "http://api.ooyala.com/partner/" + "query" + "?pcode=" + pcode + "&expires=" + GetUnixTimestamp().ToString();
PARTNER_SECRET = "embedCode=" + psecret;

SHA256Managed sh = new SHA256Managed();
byte[] request = System.Text.Encoding.ASCII.GetBytes(PARTNER_SECRET);
sh.Initialize();
byte[] b4bbuff = sh.ComputeHash(request, 0, request.Length);
string hashString = System.Text.Encoding.ASCII.GetString(b4bbuff);

string encryptedRequest = HttpUtility.UrlEncode((Convert.ToBase64String(b4bbuff)).Substring(0, 43));
url += "&" + HttpUtility.UrlEncodeUnicode("embedCode") + "=" + HttpUtility.UrlEncodeUnicode(psecret)
+ "&signature=" + encryptedRequest;