vb.net - 将图像从 asp.net (VB.net) 发布到 facebook graph api(错误 400 错误请求)

标签 vb.net facebook-graph-api httpwebrequest

我正在尝试使用 asp.net 将图像发布到 facebook grap api, 我知道这里有一篇很棒的帖子 Posting image from .NET to Facebook wall using the Graph API因为我需要 vb.net 中的代码,所以我只是转换了那里描述的代码,但是在尝试将他的图片发布到 facebook 时出现以下错误:

The remote server returned an error: (400) Bad Request.

对我来说,调试这个错误有点困难,因为这是唯一来自面部的请求,所以我不知道这个错误是因为我的表单邮政编码没有正确生成,还是错误在我的图像中正在尝试发送。

我阅读了很多关于 http 表单的帖子,但我仍然无法弄清楚我的错误在哪里......根据 Example provided by facebook我相信我确实拥有所有必需的参数。

Facebook 示例中的代码:

      // Show photo upload form to user and post to the Graph URL
     $graph_url= "https://graph.facebook.com/me/photos?"
     . "access_token=" .$access_token;
     echo '<html><body>';
     echo '<form enctype="multipart/form-data" action="'
     .$graph_url .' "method="POST">';
     echo 'Please choose a photo: ';
     echo '<input name="source" type="file"><br/><br/>';
     echo 'Say something about this photo: ';
     echo '<input name="message" 
         type="text" value=""><br/><br/>';
     echo '<input type="submit" value="Upload"/><br/>';
     echo '</form>';
     echo '</body></html>';  

我的表单生成代码(发送图片之前)

----------------------------8cf5b7942cad9d0 Content-Disposition:表单数据; name="access_token"

DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2bUKAfbTE54aBB5vdHFJaecGHPpGrLCrd5bEZWxlXvVKej0ApDbjEzjki8xzvl28etjRxH1LzcJP314RO5HJDbNbZJ ------------------------------8cf5b7942cad9d0 Content-Disposition:表单数据;名称=“消息”

Lombardi 喜欢 stackoverflow ------------------------------8cf5b7942cad9d0 Content-Disposition:表单数据;名称=“来源”;文件名="~\img\taco.jpeg" 内容类型:应用程序/八位字节流

任何帮助将不胜感激。

这是我的完整功能代码

 Private Function FB_UploadPhoto(ByVal album_id As String, ByVal message As String, ByVal filename As String, ByVal bytes As Byte(), ByVal Token As String) As String

    Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
    Dim path As String = "https://graph.facebook.com/" '& FacebookID() & "/"

    If Not String.IsNullOrEmpty(album_id) Then
        path += album_id + "/"
    End If
    path += "photos"


    Dim uploadRequest As System.Net.HttpWebRequest
    uploadRequest = CType(System.Net.HttpWebRequest.Create(path), System.Net.HttpWebRequest)
    uploadRequest.ServicePoint.Expect100Continue = False
    uploadRequest.Method = "POST"
    uploadRequest.UserAgent = "Mozilla/4.0 (compatible; Windows NT)"
    uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary
    uploadRequest.KeepAlive = False


    'New string builder

    Dim sb As New System.Text.StringBuilder


    'Add Form Data
    Dim formdataTemplate As String = "--{0}" & vbCrLf & "Content-Disposition: form-data; name=""{1}""" & vbCrLf & vbCrLf & "{2}" & vbCrLf
    'Access Token
    sb.AppendFormat(formdataTemplate, boundary, "access_token", HttpContext.Current.Server.UrlEncode(Token))
    ' Message
    sb.AppendFormat(formdataTemplate, boundary, "message", message)
    'header
    Dim headerTemplate As String = "--{0}" & vbCrLf & "Content-Disposition: form-data; name=""{1}""; filename=""{2}""" & vbCrLf & "Content-Type: {3}" & vbCrLf & vbCrLf
    sb.AppendFormat(headerTemplate, boundary, "source", filename, "application/octet-stream")
    'sb.AppendFormat(headerTemplate, boundary, "source", filename, "image/jpeg")

    Dim formString As String = sb.ToString()
    Dim formBytes As Byte() = Encoding.UTF8.GetBytes(formString)
    Dim trailingBytes As Byte() = Encoding.UTF8.GetBytes("" & vbCrLf & "--" & boundary + "--" & vbCrLf)
    Dim image As Byte()

    If bytes Is Nothing Then
        image = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(filename))
    Else
        image = bytes
    End If

    'memory stream
    Dim imageMemoryStream As New System.IO.MemoryStream()
    imageMemoryStream.Write(image, 0, image.Length)


    ' Set Content Length
    Dim imageLength As Long = imageMemoryStream.Length
    Dim contentLength As Long = formBytes.Length + imageLength + trailingBytes.Length
    uploadRequest.ContentLength = contentLength


    'Get Request Stream
    uploadRequest.AllowWriteStreamBuffering = False
    Dim strm_out As System.IO.Stream = uploadRequest.GetRequestStream()


    'Write to Stream

    strm_out.Write(formBytes, 0, formBytes.Length)

    Dim buffer As Byte() = New Byte(CType(Math.Min(4096, CType(imageLength, Integer)), UInteger)) {} 'New Byte(CUInt(imageLength)) {} ' New Byte(CUInt(Math.Min(4096, CInt(imageLength)))) {} ' 'New Byte(CUInt(Math.Min(4096, CInt(imageLength)))) {} 'New Byte(CType(Math.Min(4096, CType(imageLength, Integer)), UInteger)) {}
    Dim bytesRead As Integer = 0
    Dim bytesTotal As Integer = 0
    imageMemoryStream.Seek(0, IO.SeekOrigin.Begin)

    'While bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length) <> 0
    '    strm_out.Write(buffer, 0, bytesRead)
    '    bytesTotal += bytesRead
    'End While
    bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)
    While bytesRead <> 0
        strm_out.Write(buffer, 0, bytesRead)
        bytesTotal += bytesRead
        bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)
    End While

    strm_out.Write(trailingBytes, 0, trailingBytes.Length)

    'Close Stream
    strm_out.Close()


    'Get Web Response
    Dim response As System.Net.HttpWebResponse = uploadRequest.GetResponse()



    ' Create Stream Reader
    Dim reader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())


    Return reader.ReadToEnd()
End Function

最佳答案

真是个 blender 人……问题出在 facebook token 本身……实际上我正在加密 token ,似乎我的加密函数在解密时出现了某种问题。无论如何,我会把代码留在这里,希望其他人会觉得有用。

关于vb.net - 将图像从 asp.net (VB.net) 发布到 facebook graph api(错误 400 错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334060/

相关文章:

windows-phone-7 - Windows Phone 7上的HttpWebRequest返回 “The remote server returned an error: NotFound”

c# - MonoTouch 或 iOS 网络堆栈是否正在占用我的 HTTP DELETE 请求正文?

c# - "Constrained Types"在 VB/C# 中有用吗?

wpf - 用于转换为 MVVM 模式的事件名称

vb.net - IXmlSerializable 字典问题

java - 如何在android中使用graph api获取好友列表?

facebook - 发布到用户的 Facebook 新闻提要而不发布到他的墙上

xamarin - 升级到 Xamarin.iOS 9.8.0.323 后使用外部 IP 地址的 NameResolutionFailure

.NET - 是否可以声明只能访问命名空间的模块成员?

iphone - 如何获取适用于 iOS 的 Facebook 相册?