如何在墙上张贴图片?

时间:2011-03-31 12:44:26

标签: facebook-c#-sdk

有人成功将图片发布到当前用户的墙上吗?这是行不通的,如果图片参数是现有的图片网址没有显示!我正在使用最新的FB C#SDK 5.0.8 Beta ...

var args = new Dictionary<string, object>();
args["name"] = "My App";
args["link"] = @"http://apps.facebook.com/test/";
args["caption"] = "My Caption";
args["description"] = "My Description";
args["picture"] = @"http://www.test.com/test.jpeg";
args["message"] = "My Message";
args["actions"] = "";
FbClient.PostAsync(@"/me/feed", args);

3 个答案:

答案 0 :(得分:7)

以下是我将照片发布到Facebook用户墙上的方法。 ImagePath和ImageName是我传递给包含此代码的函数的字符串参数。

    var fbApp = new FacebookApp();
    var auth = new CanvasAuthorizer(fbApp);

    if (auth.IsAuthorized())
    {

        //Create a new dictionary of objects, with string keys
        Dictionary<string, object> parameters = new Dictionary<string, object>();

        string strDescription = txtDescription.Text;

        //Add elements to the dictionary
        if (string.IsNullOrEmpty(ImagePath) == false)
        {
            //There is an Image to add to the parameters                
            FacebookMediaObject media = new FacebookMediaObject
            {
                FileName = ImageName,
                ContentType = "image/jpeg"
            };

            byte[] img = File.ReadAllBytes(ImagePath);
            media.SetValue(img);

            parameters.Add("source", media);
            parameters.Add("message", strDescription);

            try
            {
                dynamic result = fbApp.Api("/me/photos", parameters, HttpMethod.Post);

            }
            catch (Exception ex)
            {
                //handle error....
                string strErr = ex.Message.ToString();
                lblValidationMsg.Text = strErr;
            }
        }            

    }

答案 1 :(得分:1)

这是5.0.8版中的已知错误。它已在当前来源中修复,并将在下一个版本中修复。

答案 2 :(得分:1)

您可以在用户墙中使用帖子(&#34; me / photos&#34;

    [TestMethod]
    [DeploymentItem(@".\resources\velas_navidad.gif", @".\")]
    public void Post_to_photos()
    {
        var ImagePath = "velas_navidad.gif";
        Assert.IsTrue(File.Exists(ImagePath));

        var client = new FacebookClient(token);
        dynamic parameters = new ExpandoObject();

        parameters.message = "Picture_Caption";
        parameters.subject = "test 7979";
        parameters.source = new FacebookMediaObject
        {
              ContentType = "image/gif",
              FileName = Path.GetFileName(ImagePath)
        }.SetValue(File.ReadAllBytes(ImagePath));

        dynamic result = client.Post("me/photos", parameters);

        Thread.Sleep(15000);
        client.Delete(result.id);
    }