Sharing Video To Twitter And Facebook Using ASP.NET MVC

In the current scenario, social sharing is very common and we can usually easily share things on Twitter and Facebook, using web apps. The user can post feeds, images and videos on our web apps and these will also be shared on Twitter and Facebook, if the user has an associated Twitter and Facebook with the web application.

In this article, we will learn how to share videos to Twitter and Facebook in the ASP.NET MVC Web Application. We can login also with these external login providers in our Web Application.

Getting Started

Before starting on this article, there are some prerequisites given below.

  • Visual Studio 2012 or later.
  • MVC 4 or later.
  • Creating apps on Twitter & Facebook.

Social Sharing

In this section, we’ll learn to share video, using Twitter and Facebook. I have assumed that you have created apps on Twitter & Facebook and if not then please create them. Let’s begin with Twitter:

Sharing on Twitter 

We can now share videos on Twitter with the help of a web application. TweetInvi library is the easiest way to upload the video on Twitter. We can easily get it through NuGet Package Manger. Twitter has designated some rules for uploading media.(image or video) on Twitter. You can upload only MP4 video format on Twitter. 

Note

Please read the instructions carefully on Twitter for Uploading Media/Video.

Step 1

Go to dev.twitter.com

Step 2

Click My Apps. Select your app -> Keys and Access Tokens.

You can get the Consumer key and Consumer Secret of your app, as shown below.


Similarly, on the same page, you will see your Access Token and Access Token Secret.


Step 3

Install TweetInvi library from NuGet Package to share the video on Twitter.


Step 4

We can create a new SocialShare controller in our MVC Web Application. Open the Index method in the SocialShare Controller. 

Step 5

Now, replace the code of Index method with the code given below to share the video on Twitter. 

  1. public ActionResult Index() {  
  2.     string tweetMessage = "hi";  
  3.     string filePath = @ "C:\Video\New.mp4";  
  4.     Auth.ApplicationCredentials = new TwitterCredentials("Consumer Key""Consumer Secret""Access Token""Access Token Secret");  
  5.     var videoBinary = System.IO.File.ReadAllBytes(filePath);  
  6.     var videoMedia = Upload.UploadVideo(videoBinary, "video/mp4""amplify_video");  
  7.     var status = Upload.GetMediaStatus(videoMedia); //for getting the video status  
  8.     if (status.ProcessingInfo.State == "succeeded") {  
  9.         Tweet.PublishTweet(tweetMessage, new PublishTweetOptionalParameters() {  
  10.             Medias = {  
  11.                 videoMedia  
  12.             }  
  13.         });  
  14.     }  
  15.     return View();  
  16. }   

Step 6

Now, open the _Layout.cshtml page in the Application and add the code given below to open the social page.

  1. <li>@Html.ActionLink("Index""Index""SocialShare")</li>   

Step 7

Create a view by right clicking on the Index method.


Step 8

Run the application and click on the Index link and if the video file matches with Twitter requirements, then it’ll be uploaded, as shown below.


Now, you can see the video is posted in my Twitter account with the description also.

Sharing on Facebook

We can also share the videos on Facebook with the help of a web application. Facebook Graph API is used to upload the video on Facebook. We can easily upload Resumable or Non-Resumable videos on Facebook. Facebook supports different types of formats of video as 3gp, mp4, flv etc.

Note

Please read the instructions carefully on Facebook for uploading video.

Let’s begin with the steps given below to upload the video on Facebook.

Step 1

Go to developes.facebook.com

Step 2

If you have created an app, go to Tools & support.

Now, from the next page, click on Graph API Explorer.


Note

Please note that your app must have the approval of publish_actions from Facebook. If you haven’t submitted your app for approval, please submit your app for approval.

Step 3

Click on Get User Access Token, as shown below.


Step 4

Now, select the options from the next wizard to get an access token, as shown below.


Step 5

Facebook will ask you to allow an app to publish the information and you can switch the user also from this wizard, otherwise login to Facebook.


Now, select access token from Graph API Explorer.


Step 6

Now, open the _Layout.cshtml page in the Application and add the code given below to open the social page.

  1. <li>@Html.ActionLink("Social Share"" ShareToFacebook ""SocialShare")</li>   

Step 7

Now, create another method in your HomeController to publish the video in Facebook with the help of the code given below. 

  1. public ActionResult ShareToFacebook() {  
  2.     string UserAccessToken = "Your User Access Token";  
  3.     string message = "having fun";  
  4.     string title = "hi";  
  5.     string filePath = @ "C:\Video\New.mp4";  
  6.     if (!string.IsNullOrEmpty(UserAccessToken)) {  
  7.         string FileUploadUrl = string.Format("https://graph-video.facebook.com/me/videos?title={0}&description={1}&access_token={2}", HttpUtility.UrlEncode(message), HttpUtility.UrlEncode(title), UserAccessToken);  
  8.         WebClient uploadClient = new WebClient();  
  9.         byte[] returnBytes = uploadClient.UploadFile(FileUploadUrl, filePath);  
  10.     }  
  11.     return View();  
  12. }   

Step 8

Now, your video will successfully post in Facebook, if the video meets the requirement.


Summary

In this article, we learned  how we can share or post a video on Twitter or Facebook by using ASP.NET MVC Application. Thanks.

Up Next
    Ebook Download
    View all
    Learn
    View all