Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post On Facebook Page As Page Not As Admin User Using Facebook C# SDK

I'm developing C# application which manages fan page on Facebook which uses Facebook C# SDK. I've encountered two problems one is connected with posting messages on wall and the other concerns creating events on fan page.

Is it possible to post a message on fan page wall as a fan page not a admin user ?

Can I programmatically create event on fan page (not as admin but as a fan page) using Facebook C# SDK ?

I went through some other tutorials of others SDKs such as Facebook PHP SDK. PHP SDK allows to create event as a fan page, but in case of C# SDK the creating event doesn't give any results.

like image 836
michalskuza Avatar asked Dec 28 '22 00:12

michalskuza


2 Answers

Ok, I just ran into this exact same thing. For my purposes, I'm authorizing an application to post to a FB fan page as the fan page. So I can have multiple users that have access to the application, but not access to the Fan Page, posting as the Fan Page. It works for my requirements.

Anyhow, here is how I did it using the C# Facebook SDK Beta V5.0.9. First Step, make sure the user account you are attempting to post with has the ability to post to the fan page and is authorized to do so from an app.

The majority of this is similar to VorTechS post, just a bit more clarification on it.

Dictionary<string,string> fbParams = new Dictionary<string,string>();
                fbParams["message"] = Title;
                fbParams["caption"] = string.Empty;
                fbParams["description"] = string.Empty;
                fbParams["req_perms"] = "publish_stream";
                fbParams["scope"] = "publish_stream";
                //Initialize Your Facebook Client in the manner that suits you, I did it by supplying a saved access token from a single users
                FacebookWebClient fbClient = new FacebookWebClient(<YOUR_ACCOUNT_ACCESS_TOKEN>);
                //Get the listing of accounts associated with the user
                dynamic fbAccounts = fbClient.Get("/me/accounts");

                //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
                foreach (dynamic account in fbAccounts.data) {
                    if (account.id == <DESTINATION_ID_OF_YOUR_FAN_PAGE>) {
                        //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out.
                        fbParams["access_token"] = account.access_token;
                        break;
                    }
                }
                //Then pass your destination ID and target along with FB Post info. You're Done.
                dynamic publishedResponse = fbClient.Post("/" + <DESTINATION_ID_OF_YOUR_FAN_PAGE> + "/feed", fbParams);
like image 150
Alex S. Avatar answered May 19 '23 21:05

Alex S.


For posting a message, you need to grant the manages_pages permission, and obtain an access token from the accounts for the Fan Page by using the results of "/me/accounts".

Here's what I use for posting a message itself to a Fan Page:

                            var dicParams = new Dictionary<string, object>();
                        dicParams["message"] = stSmContentTitle;
                        dicParams["caption"] = string.Empty;
                        dicParams["description"] = string.Empty;
                        dicParams["name"] = smContent.CmeUrl;
                        dicParams["req_perms"] = "publish_stream";
                        dicParams["scope"] = "publish_stream";

                        // Get the access token of the posting user if we need to
                        if (destinationID != this.FacebookAccount.UserAccountId)
                        {
                            dicParams["access_token"] = this.getPostingUserAuthToken(destinationID);
                        }
                        publishResponse = this.FacebookConnection.Post("/" + destinationID + "/feed", dicParams);
like image 44
VorTechS Avatar answered May 19 '23 22:05

VorTechS