从Silverlight上传照片到Facebook

时间:2010-09-07 00:13:14

标签: c# silverlight facebook

我最近开始将我的一个小应用程序移植到Facebook作为学习体验。我对Silverlight和.NET非常熟悉,但还没有在facebook上做过任何事情。由于所有可用的SDK和API似乎都不起作用,或者我无法正确使用它们,我决定直接访问Facebook的Graph API,到目前为止它很容易(我可以登录,请求权限,获取个人资料,专辑和照片并发布给用户提供)。现在我想上传一张照片,这就是我真正打到墙上的地方。我用这样的东西来发帖:

        WebClient client = new WebClient();
        client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
        client.UploadStringAsync(new Uri(String.Format("https://graph.facebook.com/me/feed")), "POST",
            String.Format("message={0}&link={1}&picture={2}&access_token={3}", "Test", "www.gong.bg", "http://gong.bg/uploads/teams/teams_logos/logo_small_1.png", this.Access_Token));

非常简单,但它工作正常,我不需要更多。

为了上传照片,我尝试使用类似的代码,但没有成功,那么我决定尝试使用HttpWebRequest,现在我有以下内容:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://graph.facebook.com/me/photos"));
        request.ContentType = "multipart/form-data";
        request.Method = "POST";
        request.BeginGetRequestStream(ar =>
        {
            using (StreamWriter writer = new StreamWriter((ar.AsyncState as HttpWebRequest).EndGetRequestStream(ar)))
            {
                writer.Write("{0}={1}&", "message", HttpUtility.UrlEncode("Test"));
                writer.Write("{0}=@{1}&", "source", HttpUtility.UrlEncode("3.png"));
                writer.Write("{0}={1}&", "access_token", this.Access_Token);
            }
        }, request);

这不起作用,我看不出问题出在哪里。根据Facebook文档,这应该将照片上传到应用程序默认相册(如果不存在则创建一个)

谢谢大家。

2 个答案:

答案 0 :(得分:0)

这看起来可能是一个简单的拼写错误。尝试删除最后一个&符号,以便得到这个:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://graph.facebook.com/me/photos"));
request.ContentType = "multipart/form-data";
request.Method = "POST";
request.BeginGetRequestStream(ar =>
{
    using (StreamWriter writer = new StreamWriter((ar.AsyncState as HttpWebRequest).EndGetRequestStream(ar)))
    {
        writer.Write("{0}={1}&", "message", HttpUtility.UrlEncode("Test"));
        writer.Write("{0}=@{1}&", "source", HttpUtility.UrlEncode("3.png"));
        writer.Write("{0}={1}", "access_token", this.Access_Token);
    }
}, request);

答案 1 :(得分:0)

在codeplex上试试我的Facebook .Net SDK。最新的来源支持silverlight。 http://facebooksdk.codeplex.com

你可以做你想做的事情:

        byte[] photo = File.ReadAllBytes(photoPath);
        FacebookApp app = new FacebookApp();
        dynamic parameters = new ExpandoObject();
        parameters.access_token = "access_token";
        parameters.caption = "Test Photo";
        parameters.method = "facebook.photos.upload";
        parameters.uid = ConfigurationManager.AppSettings["UserId"];
        var mediaObject = new FacebookMediaObject
        {
            FileName = "monkey.jpg",
            ContentType = "image/jpeg",
        };
        mediaObject.SetValue(photo);
        parameters.source = mediaObject;
        app.ApiAsync((ar, state) => { 
            var postId = (string)ar.Result;
        }, null, parameters, HttpMethod.Post);