A few months back, Wix released it’s beta version of Cloud Media Platform.
The service gives Microsoft’s Media Services, which rules the web in the Media encoding and post-processing, a run for their money.
While Microsoft uses their own cloud for the job, Wix is working on Google Cloud Platform to deliver their solution. The process is using the WixMP, which is very simple and requires just a Google Account to get the 2GB Cloud Storage free plan. Other pricing plans are also available.
Currently, Wix are offering SDK versions only for Javascript (Some features are only available to Node.js) and Python, and I don’t believe there will be any SDK for C#.
So to make a short story even shorter, I wrote a small helper to generate url’s to upload files from code.
In order to get the Upload url, you need to get a token from the Wix media servers,
the process is very straight forward and you can read all about it here.
To obtain the token you need your Wix API Key and Wix API Secret from the platform dashboard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.Security.Cryptography; using System.Net; using System.IO; using Newtonsoft.Json.Linq; namespace Helpers { public class WIXHelper { private string _wixAPIUploadUri = "http://mediacloud.wix.com/files/upload/url"; private string _wixAPITokenUri = "http://mediacloud.wix.com/auth/token"; private string _wixAPISecret = "myapisecret"; private string _wixAPIKey = "myapikey"; private static string GetAuthToken() { // headers vars var ts = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); var nonce = Guid.NewGuid().ToString(); // canonical signature StringBuilder canonical = new StringBuilder(); canonical.Append("GET\n"); canonical.Append("/auth/token\n"); canonical.Append("x-wix-auth-nonce:" + nonce + "\n"); canonical.Append("x-wix-auth-ts:" + ts); // create the HMACSHA256 signature and convert it to Base64 var MAC = new HMACSHA256(System.Text.Encoding.ASCII.GetBytes(_wixAPISecret)); var signature = Convert.ToBase64String(MAC.ComputeHash(System.Text.Encoding.ASCII.GetBytes(canonical.ToString()))).Replace('+', '-').Replace('/', '_'); // get token request var request = WebRequest.Create(_wixAPITokenUri); request.Headers[HttpRequestHeader.Authorization] = string.Format("WIX {0}:{1}", _wixAPIKey, signature); request.Headers.Add("x-wix-auth-nonce", nonce); request.Headers.Add("x-wix-auth-ts", ts); JObject resjson = JObject.Parse(ResponseAsDynamicJson(request)); return resjson["scheme"] + " " + resjson["token"]; } private static string StreamToString(Stream stream) { using (var sr = new StreamReader(stream)) { return sr.ReadToEnd(); } } private static WebRequest RequestBuilder(string uri) { var req = WebRequest.Create(uri); req.Headers[HttpRequestHeader.Authorization] = WIXHelper.GetAuthToken(); return req; } private static dynamic ResponseAsDynamicJson(WebRequest req) { req.Method = "GET"; var response = req.GetResponse(); return StreamToString(response.GetResponseStream()); } public static string GetMediaUploadUrl() { WebRequest requestBuilder = RequestBuilder(_wixAPIUploadUri); var response = ResponseAsDynamicJson(requestBuilder); JObject urijson = JObject.Parse(response); return urijson["upload_url"].ToString(); } } } |
Now with this helper that you can put in your code behind, calling
1 |
GetMediaUploadUrl() |
will return a url for uploading you can give back to the browser or add upload from server.
to build a form data request you will need this three keys
1. “media_type” : the type of media you want.
2. “file” : the file.
3. “user_id” : Wix API Key.
You can also reuse
1 |
RequestBuilder() |
in your own function, with other URI’s, to do more with the cloud API.
Good luck!