Monday, May 23, 2011

Upload Video to Facebook in C#

HI Folks,
If you tried to upload a video to facebook using facebook sdk in C#, You will face a problem that the provided method UploadVideo in LegacyRestApi.cs file accepts the full physical path of the file to be uploaded. For ASP.NET developers and since we are using AsyncFileUpload Control to select your files, this control provides only the file name and not the full physical path because of security constraints applied on all browsers. I have updated the source code and ask the codeplex product facebook sdk team to include this in the future release. and I'd like to share the updated code for this method in my blog for all facebook .NET Developers, Hope this helps.

C# Code:

public static string UploadVideo(string accessToken,string fileName, byte[] fileBytes)
   var mediaObject = new FacebookMediaObject 
       FileName = fileName,     
       ContentType = "video/3gpp" 
    try {         
              var fb = new FacebookClient(accessToken);     
              var parameters = new Dictionary<string, object>();   
              parameters["method"] = "video.upload";       
              parameters["caption"] = "video upload using rest api";   
              parameters["source"] = mediaObject;          
              var result = (IDictionary<string, object>)fb.Post(parameters);   
              var videoId = (string)result["vid"];        
              Console.WriteLine("Video Id: {0}", videoId);   
              // Note: This json result is not the orginal json string as returned by Facebook.    
              Console.WriteLine("Json: {0}", result.ToString());     
              return videoId;      
       catch (FacebookApiException ex) {     
            // Note: make sure to handle this exception.       
Codeplex discussion entry:


No comments: