Skip to content

Instantly share code, notes, and snippets.

@newbamboo
Created September 8, 2010 19:21
Show Gist options
  • Save newbamboo/570663 to your computer and use it in GitHub Desktop.
Save newbamboo/570663 to your computer and use it in GitHub Desktop.
struct Panda
{
public static string Api_host = "api.pandastream.com";
public static string Api_version = "/v2";
public static string Cloud_ID = "yourCloudID";
public static string Access_key = "yourAccessKey";
public static string Secret_key = "yourSecretKey";
}
[WebMethod]
public string GetVideoEncodingID_Panda()
{
string pandaResponse = string.Empty;
string videoID = "05102f9acbf2583daa85c6ec186e1635";
string pTimeStamp = GetTimestamp();
try
{
//generate string to sign
string stringToSign = "GET\n" +
Panda.Api_host + "\n" +
"/videos/" + videoID + "/encodings.json" + "\n" +
"access_key=" + Panda.Access_key +
"&cloud_id=" + Panda.Cloud_ID +
"&timestamp=" + pTimeStamp;
string signature = EncodeStringToSignAsHMACSHA256(stringToSign, Panda.Secret_key);
//generate request string
string callUrl = "http://" + Panda.Api_host +
Panda.Api_version +
"/videos/" + videoID + "/encodings.json?" +
"access_key=" + HttpUtility.UrlEncode(Panda.Access_key) +
"&cloud_id=" + HttpUtility.UrlEncode(Panda.Cloud_ID) +
"&timestamp=" + pTimeStamp +
"&signature=" + HttpUtility.UrlEncode(signature);
//call Panda API
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(callUrl);
httpReq.Method = "GET";
httpReq.ContentLength = 0;
HttpWebResponse httpResponse = (HttpWebResponse)httpReq.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader responseStream = new StreamReader(httpResponse.GetResponseStream(), enc);
pandaResponse = responseStream.ReadToEnd();
}
catch (Exception ex)
{
}
return pandaResponse;
}
public string EncodeStringToSignAsHMACSHA256(string stringToSign, string secretKey)
{
byte[] toSign = Encoding.UTF8.GetBytes(stringToSign);
byte[] secret;
HMAC signer;
secret = Encoding.UTF8.GetBytes(secretKey);
signer = new HMACSHA256(secret);
byte[] sigBytes = signer.ComputeHash(toSign);
string signature = Convert.ToBase64String(sigBytes);
return signature;
}
private string GetTimestamp()
{
DateTime currentTime = DateTime.UtcNow;
string timestamp = currentTime.ToString("yyyy-MM-ddTHH:mm:ss+00:00");
return HttpUtility.UrlEncode(timestamp).ToUpper();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment