Jaa


Automate Posts to Facebook Fan Page with .NET and Microsoft Azure

Join my mailing list! Download the sample project on GitHub  |  Follow me on Twitter at @nickpinheir

Thus far in my Facebook Development blog series we have covered a number of topics.  We have covered Facebook Login, which provides the underpinnings for all subsequent Facebook integration.  We have covered how to capture a user’s profile information and more importantly their email address.  Furthermore, we have also covered how to programmatically post to a user’s Timeline and to News Feed using the publish_actions permission.  These tools will be of extreme value as you continue on in the process of building, growing and monetizing your apps.

Now as you build out your app, whether web or mobile, a common tool to promote it will be a Facebook Fan Page.  A Fan Page is a publically facing page on Facebook used to build a closer relationship with your audience and customers.  Below is a screenshot of the Fan Page for ‘MSDevz’, our sample app which will be used throughout the Facebook Development series.  MSDevz is a social web and mobile app for Microsoft developers that aggregates content from the following providers:  Facebook, Instagram, YouTube, Bing, iTunes and Amazon.

One of the core features of MSDevz is the ability to automate the capture of news from the Azure Data Marketplace’s Bing Search API.  The solution uses an Azure WebJob which runs hourly and captures data on Microsoft development news topics.  These article references are then inserted into to MSDev’s Azure SQL Database and displayed within the app.

Now what if we wanted to take things one step further?  What if we wanted to not only automate the capture of information from the Bing Search API, but also post to MSDev’s Facebook Fan Page each time a new article is added.  To do so we would need to develop a HttpPost to the particular edge (endpoint) of the Facebook Graph API which allows you to publish to a Fan Page.

Permission

In order to post to a Facebook Fan Page we will need to generate an ‘page’ access token which includes the publish_pages permission to publish new posts on behalf of that page. Posts will appear in the voice of the page.  Note this a different type of access token from the user access token which we previously received by using Facebook Login.  In order to generate a page access token we will need to use the Facebook Graph API Explorer tool.

Access Token

Using the tool, under the ‘Applications’ dropdown we must select the Facebook Application we have created in the developer portal (MSDevz).  Directly below in the next dropdown, we select the Fan Page we are requesting a page access token for (MSDevz).  Immediately upon selection, the tool will generate an access token for us in the ‘Access Token’ field.  While this would give us the permissions we need, the generated access token only has a lifespan of one hour.  Therefore, in order to supply our code with a long lived token, we must use the ‘Open in Access Token Tool’ to generate an extended token which will have a lifespan of 60 days.  See screenshots below:

Graph API

Now that we have an extended page access token, we are ready to write our code to programmatically post to our Facebook Fan Page.  To do so we will create a ‘Azure WebJob’ project in Visual Studio.  If you do not currently see this as an option in your File > New > Project dialogue, you must install the latest version of the Azure SDK.  At the time of this writing, the latest version of the SDK is 2.8.1.  Note, our code will NOT require an Azure subscription to run, an Azure WebJob project is essentially a console application which can be deployed to Azure.  Therefore, you will be able to debug and run you console application locally in Visual Studio and programmatically post to your Facebook Fan Page.

Once our project is created, the first thing we will do is open our App.config file and enter settings for our ‘Facebook Page Id’ and our ‘Facebook Page Access Token’:

Click save and we are ready to add our code.  For the purposes of this example we will simply generate one programmatic post to our Facebook Fan Page.  Keep in mind that this will be the core of what you will use if you would like to later automate posts.  Simply iterate on your source of content and post to the Facebook Fan Page passing in the relevant parameters.

In order to publish a HttpPost to our Facebook Graph API edge (endpoint), we will use the .NET HttpClient.  The edge we will be posting to is ‘/{page-id}/feed’.  The following code is all we need to process the post.  The sample project is available on GitHub: https://github.com/nickpinheiro/FacebookFanPagePosts

All we need to do now is click ‘Start’ to run the console application.  Provide the post is successfully posted to Facebook in our console window we will receive a response from the Facebook service with the id of the new post:

And there we have it!  The following is our Facebook Fan Page post on the MSDevz Fan Page:

Download the sample project on GitHub: https://github.com/nickpinheiro/FacebookFanPagePosts

Resources:

Other posts in the Facebook Development series:

 

  Nick Pinheiro is an experienced software engineer, technologist, speaker, author and evangelist. He is a Senior Consultant in the Azure Applications Circle of Excellence (CoE) at Microsoft where he develops cloud-based, web and mobile applications for the world’s largest organizations. He speaks at various industry conferences on Modern Apps, Social and Cloud. Follow him on Twitter at @nickpinheir.

 

Join my mailing list!