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

时间:2012-09-08 20:07:59

标签: 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时我收到以下错误:

  

远程服务器返回错误:(400)错误请求。

对我来说,这是一个很难调试这个错误,因为这是从面部来的唯一请求因此我不知道错误是因为我的表单邮政编码没有正确生成或错误是图像中的错误我我试图发送。

我正在阅读很多关于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 内容处理:表格数据;名称= “的access_token”

DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2bUKAfbTE54aBB5vdHFJaecGHPpGrLCrd5bEZWxlXvVKej0ApDbjEzjki8xzvl28etjRxH1LzcJP314RO5HJDbNbZJ ----------------------------- 8cf5b7942cad9d0 内容处理:表格数据;名称= “消息”

Lombardi喜欢stackoverflow ----------------------------- 8cf5b7942cad9d0 内容处理:表格数据; NAME = “源”;文件名= “〜\ IMG \ taco.jpeg” Content-Type:application / octet-stream

任何帮助都将受到高度赞赏。

这是我的完整功能代码

 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

1 个答案:

答案 0 :(得分:1)

什么是一个混蛋男人...问题是facebook令牌本身......实际上我加密令牌似乎我的加密功能在解密时遇到了某种问题。无论如何,我会把代码留在这里,希望其他人会觉得有用。