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

后端 未结 4 844
北海茫月
北海茫月 2021-01-01 04:10

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 a

4条回答
  •  耶瑟儿~
    2021-01-01 04:49

    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=&redirect_uri=&scope=offline_access,manage_pages,publish_stream&response_type=token
        string myAccessToken = "";
        string myPageId = "";
    
        Dictionary fbParams = new Dictionary();
        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

提交回复
热议问题