How to post photo to facebook page feed - not posted by others

谁说我不能喝 提交于 2019-12-12 19:25:27

问题


I have installed facebook sdk

Now i am able to post to wall like this with below code

the code for posting above message

        FacebookClient myClient = new FacebookClient(srAcceToken);
        var dicParams = new Dictionary<string, object>();
        dicParams["message"] = "trial message";
        dicParams["caption"] = string.Empty;
        dicParams["description"] = string.Empty;
        dicParams["name"] = "deneme 123";
        dicParams["req_perms"] = "publish_stream";
        dicParams["scope"] = "publish_stream";
        dicParams["access_token"] = srAcceToken;
        var publishResponse = myClient.Post("/" + srPageId + "/feed", dicParams);

Now the problem occurs when i want to post image. When i post with using below code i am getting this image

the code for getting above result

            var mediaObject = new FacebookMediaObject
        {
            ContentType = "image/jpeg",
            FileName = "pokemon.jpg"
        }.SetValue(File.ReadAllBytes(Imagepath));

        FacebookClient myClient = new FacebookClient(srAcceToken);
        var dicParams = new Dictionary<string, object>();
        dicParams["message"] = "trial message";
        dicParams["caption"] = string.Empty;
        dicParams["description"] = string.Empty;
        dicParams["name"] = "deneme 123";
        dicParams["req_perms"] = "publish_stream";
        dicParams["scope"] = "publish_stream";
        dicParams["source"] = mediaObject;
        dicParams["type"] = "normal";
        dicParams["access_token"] = srAcceToken;
        var publishResponse = myClient.Post("/" + srPageId + "/photos", dicParams)

But i want to post image as below - how can i do it ?

c# 4.5 - using latest facebook sdk thank you


回答1:


hope this short code will help you

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

===================================

fbParams["access_token"] = account.access_token;

this line will get you access to write on this page



来源:https://stackoverflow.com/questions/17157808/how-to-post-photo-to-facebook-page-feed-not-posted-by-others

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