Post status message & Images on Facebook using Graph API & C#

Realizing the importance of social media, in previous blog we learnt to integrate twitter to our C#.Net application. We saw “How to post message and image on Twitter”. Now let’s learn how to post messages & images on Facebook account as well as Facebook Page.

How to use to post messages and Images on Facebook Account & Page using ASP.Net C#

 For integrating Facebook to your application we need to generate the keys & access token to authenticate the application.

Generate keys & access token

1. Sign In at “https://www.facebook.com” using your Facebook login id.

2. After that browse to the “https://developers.facebook.com” page.

Register as a Developer

Register as a Developer

3. Accept it by selecting the option to “YES” in the dialog box opened.

Accept the Facebook Platform Policy

Accept the Facebook Platform/Private Policy

  

4. After clicking on Register you will get this screen, click on “Done” to continue.

Get Confirmation After Registration

Get Confirmation After Registration

 

5. After clicking “Done” you will be redirected to this screen, you can develop the app which will be integrated to Facebook. Select the type to app you want to develop.

Add A New App

Add A New App

 

6. I have used website as I will be connecting to web app. Create new app as shown in the image.

Create Facebook App ID

Create Facebook App ID

 

7. After clicking on “Create New Facebook App ID”, you will get below screen where you have to select the category of your application.

Choose Category Of Application

Choose Category Of Application

 

8. After creating App ID, you will get the SDK script as shown below which can be used on page load.

Facebook SDK for Javascript

Facebook SDK for Javascript

 

9. After clicking on “Skip Quick Start”, you will be redirected to dashboard.

App Dashboard

App Dashboard

 

10. Go to “Tools & Support” menu to retrieve Access Token.

Tool & Support Menu

Tool & Support Menu

 

11. Here you will get the User & Application access tokens of you apps created.

User & App Access Token

User & App Access Token

 

12. Click on “need to grant permission” to assign permission to your application and retrieve user access token.

Access Token Creation Confirmation

Access Token Creation Confirmation

 

13. You will get the User access token here and you can debug and check if it’s working or expired as shown below.

Debug User & App Access Token

Debug User & App Access Token

 

14. After clicking debug beside “User Token” you will get the access token information as shown below.

Access Token Debugger & Info

Access Token Debugger & Info

 

15. After clicking debug beside “App Token” you will get App token & its id with name as shown below.

Access Token Debugger & Info

Access Token Debugger & Info

 

16. Go To “Graph Api Explorer” menu from “Tools & Support” menu and you will get the below screen from which you can user access token & debug it to check if it is valid as well as you can get data, post data  or delete the data.

Graph API Explorer

Graph API Explorer

 

17. You can select the Account/Page from “Get Token” drop down to retrieve its user access token(For first time it will only show Get Access Token and Page Access Token as shown below) you can click here and retrieve the access token.

Get Access Token

Get Access Token

 

18. Assign user data permissions and extended permissions as shown below.

User Data Permissions

User Data Permissions

 

 

Extended Permissions

Extended Permissions

 

19. After clicking get access token you will get the below screen. Click okay and you will get the user access token.

Graph API Explorer Authorities confirmation

Graph API Explorer Authorities confirmation

 

20. Copy the access token and you can use this in your application.

Access Token

Access Token

 

21. Retrieve album id as this will help to retrieve album information as well as you can post photos into this album with the help of this id.

Get Album ID Using Graph API

Get Album ID Using Graph API

 

22. Retrieve the account/page id as well as album id of that account/page.

  • Select you page
Choose Facebook Account/Page

Choose Facebook Account/Page

 

  • Retrieve the page id
Account/Page ID

Account/Page ID

 

  • Retrieve the desired album id from the page so that you can post photos into that album
Get Album ID Using Graph API

Get Album ID Using Graph API

 

Refer the next blog for the code to post message, images and albums on Facebook using above keys access tokens and id’s. 

 

Get Facebook SDK for C# from nuget

1. Download NuGet manager.

2. Go to visual studio >Tools > NuGet Package Manager > “Package Manager Console 

3. Execute the command “Install-Package Facebook” as shown below

Code to create album on Facebook account using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

 

// ————————set Access Token
string  AccessToken = <AccessToken>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(<AccessToken>);

// ————————set the parameters
var albumParameters = new Dictionary<string, object>();
string albumName = “Album Name”;
string albumMessage = “Album Message”;
albumParameters["message"] = albumMessage;
albumParameters["name"] = albumName;

 // ————————create album on facebook account
dynamic result =             facebookClient.Post(“/me/albums/”, albumParameters);

// ————————you can use this id to post photos into it
string id = result.id;

Code to create album on Facebook Page using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token
string  PageAccessToken = <PageAccessToken>;
string PageID = <Page ID>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(<PageAccessToken>);

// ————————set the parameters
var albumParameters = new Dictionary<string, object>();
string albumName = “Album Name”;
string albumMessage = “Album Message”;
albumParameters["message"] = albumMessage;
albumParameters["name"] = albumName;

// ————————create album on facebook account
dynamic result = facebookClient.Post(“/” + PageID + “/albums/”, albumParameters);

// ————————you can use this id to post photos into it
string id = result.id;

Code to post messages on Facebook account using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token
string  AccessToken = <AccessToken>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(<AccessToken>);

// ————————set the parameters
var parameters = new Dictionary<string, object>();
parameters["message"] = “<Message>”;
facebookClient.Post(“/me/feed”, parameters);

Code to post Image with message on Facebook account using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token, Album ID & Path
string  AccessToken = <AccessToken>;
string fpath=<fpath>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(AccessToken);

// ————————set the parameters
byte[] photo = File.ReadAllBytes(fpath);
dynamic parameters = new ExpandoObject();
parameters.access_token = AccessToken;
parameters.message = “<Message>”;
var mediaObject = new FacebookMediaObject
{
ContentType = “image/jpg”,
FileName = Path.GetFileName(fpath)
};
mediaObject.SetValue(photo);
parameters.source = mediaObject;

// ————————Post on Facebook
facebookClientPost(“/me/photos”, parameters);

Code to post Image with message on Facebook account in particular album using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token, Album ID & Path
string  AccessToken = <AccessToken>;
string albumId= <albumid>;
string fpath=<fpath>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(AccessToken);

// ————————set the parameters
byte[] photo = File.ReadAllBytes(fpath);
dynamic parameters = new ExpandoObject();
parameters.access_token = AccessToken;
parameters.message = “<Message>”;
var mediaObject = new FacebookMediaObject
{
ContentType = “image/jpg”,
FileName = Path.GetFileName(fpath)
};
mediaObject.SetValue(photo);
parameters.source = mediaObject;

// ————————Post on Facebook
facebookClient.Post(“/” + albumId + “/photos”, parameters);

Code to post messages on Facebook Page using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token
string  PageAccessToken = <PageAccessToken>;
string PageID = <PageID>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(<PageAccessToken >);

// ————————set the parameters
var parameters = new Dictionary<string, object>();
parameters["message"] = “<Message>”;
facebookClient.Post(“/”+ PageID+”/feed”, parameters);

Code to post Image with message on Facebook Page using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token, Album ID & Path
string  PageAccessToken = < PageAccessToken >;
string fpath=<fpath>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(PageAccessToken);

// ————————set the parameters
byte[] photo = File.ReadAllBytes(fpath);
dynamic parameters = new ExpandoObject();
parameters.access_token = AccessToken;
parameters.message = “<Message>”;
var mediaObject = new FacebookMediaObject
{
ContentType = “image/jpg”,
FileName = Path.GetFileName(fpath)
};
mediaObject.SetValue(photo);
parameters.source = mediaObject;

// ————————Post on Facebook
facebookClient.Post(“/”+PageID+”/photos”, parameters);

Code to post Image with message on Facebook Page in particular album using ASP.NET C#.

1. Added the reference and namespace “using Facebook”;

2. Write the below code:

// ————————set Access Token, Album ID & Path
string  PageAccessToken = < PageAccessToken >;
string albumId= <albumid>;
string fpath=<fpath>;

// ————————create the FacebookClient object
FacebookClient facebookClient = new FacebookClient(PageAccessToken);

// ————————set the parameters
byte[] photo = File.ReadAllBytes(fpath);
dynamic parameters = new ExpandoObject();
parameters.access_token = AccessToken;
parameters.message = “<Message>”;
var mediaObject = new FacebookMediaObject
{
ContentType = “image/jpg”,
FileName = Path.GetFileName(fpath)
};
mediaObject.SetValue(photo);
parameters.source = mediaObject;

// ————————Post on Facebook
facebookClient.Post(“/” + albumId + “/photos”, parameters);