VB.NET 使用 Graph API 将照片上传到 Facebook

发布于 2024-10-08 13:22:37 字数 515 浏览 1 评论 0原文

寻找一些代码来使用 VB.NET 中的 Graph API 将照片上传到 Facebook。我有 Facebook C# SDK,但据我所知,它不支持上传照片。

访问照片效果很好,我也可以将其他内容发送到 Facebook。只是不是照片。

facebook 文档讨论了将文件作为表单多部分请求附加,但我不知道该怎么做。说它没有很好的记录是轻描淡写的。即使是我雇来做这种事情的人也无法让它发挥作用。

我发现了这个:使用 Facebook 的 Graph API 将照片上传到相册,但它只描述了如何在 PHP 中执行此操作。

我还看到来自不同站点的关于将照片的 URL 作为 HTTP 请求的一部分传递的不同方法,但在多次尝试本地或远程 URL 后,我不断收到错误的 url 错误或类似的错误。

有什么想法吗?

Looking for some code to upload a photo to facebook using the Graph API in VB.NET. I have the Facebook C# SDK, but it doesn't support uploading photos as far as I can tell.

Accessing the photos works fine and I can send other content to Facebook fine as well. Just not photos.

The facebook documentation talks about attaching the file as a form-multipart request, but I have no idea how to do that. To say that its not very well documented is to put it lightly. Even the guys I hire to do this kind of thing couldn't get it to work.

I've found this: Upload Photo To Album with Facebook's Graph API, but it only describes how to do it in PHP.

I've also seen varying method from different sites about passing the URL of the photo as a part of the HTTP request, but after trying local or remote URLs several times I kept getting a bad url error or something like that.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-10-15 13:22:37

您需要将 POST 请求中的图像传递给 Graph API(需要publish_stream 权限)。 Facebook 文档中提到的内容是正确的。以下是可以完成这项工作的示例代码。在方法内部使用它。 (代码为 C#)

图例
:您需要提供信息。

更新
请发表评论以改进代码。

string ImageData;
string queryString = string.Concat("access_token=", /*<Place your access token here>*/);
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);

StringBuilder sb = String.Empty;
sb.Append("----------").Append(boundary).Append("\r\n");
sb.Append("Content-Disposition: form-data; filename=\"").Append(/*<Enter you image's flename>*/).Append("\"").Append("\r\n");
sb.Append("Content-Type: ").Append(String.Format("Image/{0}"/*<Enter your file type like jpg, bmp, gif, etc>*/)).Append("\r\n").Append("\r\n");

using (FileInfo file = new FileInfo("/*<Enter the full physical path of the Image file>*/"))
{
    ImageData = file.OpenText().ReadToEnd();
}
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
byte[] fileData = Encoding.UTF8.GetBytes(ImageData);
byte[] boundaryBytes = Encoding.UTF8.GetBytes(String.Concat("\r\n", "----------", boundary, "----------", "\r\n"));
var postdata = new byte[postHeaderBytes.Length + fileData.Length + boundaryBytes.Length];
Buffer.BlockCopy(postHeaderBytes, 0, postData, 0, postHeaderBytes.Length);
Buffer.BlockCopy(fileData, 0, postData, postHeaderBytes.Length, fileData.Length);
Buffer.BlockCopy(boundaryBytes, 0, postData, postHeaderBytes.Length + fileData.Length, boundaryBytes.Length);

var requestUri = new UriBuilder("https://graph.facebook.com/me/photos");
requestUri.Query = queryString;
var request = (HttpWebRequest)HttpWebRequest.Create(requestUri.Uri);
request.Method = "POST";
request.ContentType = String.Concat("multipart/form-data; boundary=", boundary);
request.ContentLength = postData.Length;

using (var dataStream = request.GetRequestStream())
{
      dataStream.Write(postData, 0, postData.Length);
}

request.GetResponse();

You need to pass the Image in a POST request to the Graph API (Need publish_stream permission). What is mentioned in Facebook Documentation is correct. Following is the example code that may do the work. use it inside a method. (Code is in C#)

Legend
<content> : you need to provide the info.

Update
Please post comments to improve the code.

string ImageData;
string queryString = string.Concat("access_token=", /*<Place your access token here>*/);
string boundary = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);

StringBuilder sb = String.Empty;
sb.Append("----------").Append(boundary).Append("\r\n");
sb.Append("Content-Disposition: form-data; filename=\"").Append(/*<Enter you image's flename>*/).Append("\"").Append("\r\n");
sb.Append("Content-Type: ").Append(String.Format("Image/{0}"/*<Enter your file type like jpg, bmp, gif, etc>*/)).Append("\r\n").Append("\r\n");

using (FileInfo file = new FileInfo("/*<Enter the full physical path of the Image file>*/"))
{
    ImageData = file.OpenText().ReadToEnd();
}
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
byte[] fileData = Encoding.UTF8.GetBytes(ImageData);
byte[] boundaryBytes = Encoding.UTF8.GetBytes(String.Concat("\r\n", "----------", boundary, "----------", "\r\n"));
var postdata = new byte[postHeaderBytes.Length + fileData.Length + boundaryBytes.Length];
Buffer.BlockCopy(postHeaderBytes, 0, postData, 0, postHeaderBytes.Length);
Buffer.BlockCopy(fileData, 0, postData, postHeaderBytes.Length, fileData.Length);
Buffer.BlockCopy(boundaryBytes, 0, postData, postHeaderBytes.Length + fileData.Length, boundaryBytes.Length);

var requestUri = new UriBuilder("https://graph.facebook.com/me/photos");
requestUri.Query = queryString;
var request = (HttpWebRequest)HttpWebRequest.Create(requestUri.Uri);
request.Method = "POST";
request.ContentType = String.Concat("multipart/form-data; boundary=", boundary);
request.ContentLength = postData.Length;

using (var dataStream = request.GetRequestStream())
{
      dataStream.Write(postData, 0, postData.Length);
}

request.GetResponse();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文