Facebook+Video+Upload

Sample code for illustration purpose. It shows how to construct upload message.

Class level constant

code format="vbnet" Public Const PREFIX As String = "--" code

When Page Loads,

code format="vbnet" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Page.IsPostBack Then Dim myfile As HttpPostedFile = Request.Files(0) Dim myStream As Stream = myfile.InputStream Uploadme(myStream, myfile.FileName) End If

code

UploadMe function. Use this function to do the actual upload.

code format="vbnet" Public Sub Uploadme(ByVal mystream As Stream, ByVal nameoffile As String)

If fbconnect.isAuthenticated Then Try

Dim myParametersToSendToFB As New SortedDictionary(Of String, String) myParametersToSendToFB.Add("api_key", fbconnect.APIKEY) myParametersToSendToFB.Add("call_id", System.DateTime.Now.Ticks.ToString)

myParametersToSendToFB.Add("v", "1.0") myParametersToSendToFB.Add("method", "facebook.video.upload") myParametersToSendToFB.Add("session_key", fbconnect.SessionID) myParametersToSendToFB.Add("sig", ComputeHash(myParametersToSendToFB, fbconnect.AppSecret))

Dim myResponseDataAsXml As String Dim myPostBoundary As String = DateTime.Now.Ticks.ToString("x", Globalization.CultureInfo.InvariantCulture) Dim myReqUrl As String = "http://api-video.facebook.com/restserver.php" Dim sb As New StringBuilder For Each myParamsIterator In myParametersToSendToFB sb.Append(PREFIX).Append(myPostBoundary).Append(ControlChars.CrLf) sb.Append("Content-Disposition: form-data; name=""").Append(myParamsIterator.Key).Append("""") sb.Append(ControlChars.CrLf) sb.Append(ControlChars.CrLf) sb.Append(myParamsIterator.Value) sb.Append(ControlChars.CrLf) Next

sb.Append(PREFIX).Append(myPostBoundary).Append(ControlChars.CrLf) sb.Append("Content-Disposition: form-data; filename=""").Append(nameoffile).Append("""").Append(ControlChars.CrLf) sb.Append("Content-Type: ").Append("video/avi").Append(ControlChars.CrLf).Append(ControlChars.CrLf)

Dim postheaderbytes As Byte = Encoding.UTF8.GetBytes(sb.ToString) Dim streamlength As Integer = Convert.ToInt32(mystream.Length)

Dim fileData(streamlength) As Byte mystream.Read(fileData, 0, streamlength) mystream.Close

Dim boundaryBytes As Byte = Encoding.UTF8.GetBytes(String.Concat(ControlChars.CrLf, PREFIX, myPostBoundary, PREFIX, ControlChars.CrLf))

Dim myWebReq As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(myReqUrl), HttpWebRequest) myWebReq.ContentLength = postheaderbytes.Length + fileData.Length + boundaryBytes.Length myWebReq.ContentType = String.Concat("multipart/form-data; boundary=", myPostBoundary) myWebReq.Method = "POST"

Dim myWebReqStream As Stream = myWebReq.GetRequestStream

myWebReqStream.Write(postheaderbytes, 0, postheaderbytes.Length) myWebReqStream.Write(fileData, 0, fileData.Length) myWebReqStream.Write(boundaryBytes, 0, boundaryBytes.Length) myWebReqStream.Close

Dim myResponse = DirectCast(myWebReq.GetResponse, HttpWebResponse) Dim myStreamReader As New StreamReader(myResponse.GetResponseStream)

myResponseDataAsXml = myStreamReader.ReadToEnd myStreamReader.Close

Dim myXMLDOC As New System.Xml.XmlDocument myXMLDOC.LoadXml(myResponseDataAsXml)

Dim myhyperlink As New HyperLink myhyperlink.Text = "Watch" myhyperlink.NavigateUrl = String.Format("http://www.facebook.com{0}", myXMLDOC.DocumentElement.ChildNodes(1).InnerText)

form1.Controls.Add(myhyperlink)

Catch ex As Exception

End Try

End If   End Sub code

ComputeHash method

code format="vbnet" Private Function ComputeHash(ByVal myparams As SortedDictionary(Of String, String), ByVal AppSecretKey As String) As String Dim parametersForSig As New StringBuilder For Each myKey As String In myparams.Keys parametersForSig.Append(String.Format("{0}={1}", myKey, myparams(myKey))) Next parametersForSig.Append(AppSecretKey) Dim myMD5 As New System.Security.Cryptography.MD5CryptoServiceProvider Dim myData As Byte = myMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(parametersForSig.ToString))

Dim md5HashToReturn As New StringBuilder Dim i As Integer = 0 For i = 0 To myData.Length - 1 md5HashToReturn.Append(myData(i).ToString("x2"))

Next Return md5HashToReturn.ToString.ToLower

End Function

code

We are using FBConnect Class in this example.

The complete source code may be downloaded from Google Project Hosting