Home > Uncategorized > Post a #YouTube video to #Facebook via C#

Post a #YouTube video to #Facebook via C#


This post demonstrates how to post Video content to a Facebook page automatically, for example on https://www.facebook.com/Petrucci-Sheet-Music-1109694145794629/ – Here I am assuming you have a Faceook page set up, you know it’s Page Id, and you have a long-lived access token, and an API Key for Google, with the YouTube API enabled.

Install the Youtube Nuget package using

Install-Package Google.Apis.YouTube.v3

Then add this code to get videos;

private static SearchListResponse GetVideos(string searchTerm)
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
ApiKey = “{{API key}}”,
ApplicationName = “Test Application”

var searchListRequest = youtubeService.Search.List(“snippet”);
searchListRequest.Q = searchTerm;
searchListRequest.MaxResults = 50;
searchListRequest.Type = “video”;

// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = searchListRequest.Execute();
return searchListResponse;

Then this code to post to facebook

static async Task PostToFacebookAsync(string searchTerm, string accessToken, string pageId, string suffix)
// Use HttpClient
using (var client = new HttpClient())
// Set variable values for post to facebook
string uri = string.Format(“https://graph.facebook.com/{0}/feed?”, pageId);

var results = GetVideos(searchTerm);
var rand = new Random();
var result = results.Items[rand.Next(results.Items.Count)];

var strName = HttpUtility.UrlEncode(result.Snippet.Title + ” ” + result.Snippet.Description + suffix);
var strLink = HttpUtility.UrlEncode(“http://youtu.be/” + result.Id.VideoId);
// Formulate querystring for graph post
StringContent queryString = new StringContent(“access_token=” + accessToken + “&message=” + strName + “&link=” + strLink);

// Post to facebook /{page-id}/feed edge
HttpResponseMessage response = await client.PostAsync(new Uri(uri), queryString);
if (response.IsSuccessStatusCode)
// Get the URI of the created resource.
string postId = await response.Content.ReadAsStringAsync();
Console.WriteLine(“Post Successfully: Your Post ID is: {0}”, postId);

string postId = await response.Content.ReadAsStringAsync();
Console.WriteLine(“Something Going Wrong”);


and you call it like so;

PostToFacebookAsync(strSearchTerm, strAccessToken, strPageId, strSuffix).Wait();

Categories: Uncategorized
  1. searchextra
    November 18, 2017 at 12:03 pm

    how can i list the result in listview or textbox or etc..


  2. searchextra
    November 18, 2017 at 12:05 pm

    how can write to resuts in uwp listview or textbox or any etc…


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: