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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 15:19:27

问题


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.


回答1:


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);



回答2:


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);



回答3:


I want to thank you for your post, it was really helpful. For me, it was still posting on my Facebook page as ME (not as page). I think it is just the way i get the token. Although, the foreach loop was not working for me. So, I did this:

public void postFacebook(object sender, EventArgs e)
{
    //You will get your "myAccessToken" at this adress:
    //https://www.facebook.com/dialog/oauth?client_id=<YOUR_APP_ID>&redirect_uri=<THE_PAGE_YOU_WILL_RECEIVE_YOUR_TOKEN_AT>&scope=offline_access,manage_pages,publish_stream&response_type=token
    string myAccessToken = "<IN_THE_RETURNED_URL_BELOW>";
    string myPageId = "<YOUR_FAN_PAGE_ID>";

    Dictionary<string,string> fbParams = new Dictionary<string,string>();
    fbParams["message"] = "Testing 1 2 test";
    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
    Facebook.FacebookAPI fbClient = new Facebook.FacebookAPI(myAccessToken);
    //Get the listing of accounts associated with the user
    var fbAccounts = fbClient.Get("/me/accounts");

    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
    foreach (var account in fbAccounts.Dictionary["data"].Array)
    {
        if (account.Dictionary["id"].String == myPageId)
        {
            //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.Dictionary["access_token"].String;

            //Update the Access token (to post as the page). If you don't it will post as YOUR personal name.
            fbClient.AccessToken = fbParams["access_token"];
            break;
        }
    }



    //Then pass your destination ID and target along with FB Post info. You're Done.
    dynamic publishedResponse = fbClient.Post("/" + myPageId + "/feed", fbParams);
}

By the way, you will need this sdk: SDK




回答4:


in MVC4 and C# SDK, this got my goal

  [FacebookAuthorize("publish_stream")]
    public ActionResult PostNotification(FacebookContext context)
    {
        var dicParams = new Dictionary<string, object>();
        dicParams["message"] = "đang tập code cho facebook ~@@";
        dicParams["caption"] = "bbbbb";
        dicParams["description"] = "cccccc";
        dicParams["name"] = "http://www.facebook.com";
        dicParams["req_perms"] = "publish_stream";
        dicParams["scope"] = "publish_stream";
        string destinationID = context.UserId;
        context.Client.Post("/" + destinationID + "/feed", dicParams);
        return RedirectToAction("Index");
    }


来源:https://stackoverflow.com/questions/5231827/post-on-facebook-page-as-page-not-as-admin-user-using-facebook-c-sharp-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!