从Silverlight上传照片到Facebook
我最近开始将我的一个小应用程序移植到Facebook作为学习体验。我对Silverlight和.NET非常熟悉,但尚未在Facebook上做任何事情。由于所有可用的SDK和API似乎都不起作用,或者我无法正确使用它们,所以我决定直接访问Facebook的Graph API,并且这很容易(我可以登录,请求权限,获取个人资料,相册和照片并发布到用户供稿)。现在我想上传一张照片,这就是我真正打的墙。我使用类似的东西来发布饲料:从Silverlight上传照片到Facebook
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}[email protected]{1}&", "source", HttpUtility.UrlEncode("3.png"));
writer.Write("{0}={1}&", "access_token", this.Access_Token);
}
}, request);
这不是工作,我看不出哪里有问题是。根据Facebook的文件,这应该上传一张照片到应用程序的默认相册(创建一个,如果不存在)
谢谢大家。
尝试我的Facebook上的.Net SDK Codeplex。最新的源代码支持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);
看起来这可能是一个简单的错字。尝试删除最后一个符号,所以你得到这个:
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}[email protected]{1}&", "source", HttpUtility.UrlEncode("3.png"));
writer.Write("{0}={1}", "access_token", this.Access_Token);
}
}, request);
是,这确实是一个错字,但修复它并没有解决问题。 – 2010-09-07 00:57:27
1.这不是如何 “的multipart/form-data的” 后的作品。 2.我知道至少有三个不同的Silverlight库支持照片上传到Facebook。你确定要实施自己的? – Denis 2010-09-07 02:05:16
不,我不确定,但我已经拥有了其他所有东西,并且需要一些努力来移动它。但是无论如何,你会把我指向这些图书馆中的一些,也许我错过了一些东西。另外,“多部分/表单数据”如何工作? – 2010-09-07 07:17:44
几个指针http://www.ietf.org/rfc/rfc1867.txt,http://www.faqs.org/rfcs/rfc2388.html,http://www.vivtek.com/rfc1867.html – Denis 2010-09-07 22:41:33