我正在尝试使用 asp.net 将图像发布到 facebook grap api,我知道这里有一篇很棒的帖子 使用 Graph API 从 .NET 将图像发布到 Facebook 墙上,因为我需要 vb.net 中的代码我只是转换了那里描述的代码,但是在尝试将他的图片发布到 facebook 时,我收到以下错误:
远程服务器返回错误:(400) 错误请求。
对我来说,调试这个错误有点困难,因为这是来自面部的唯一请求,因此我不知道错误是因为我的表单邮政编码没有正确生成还是错误在我正在尝试的图像中发送。
我正在阅读很多关于 http 表单帖子的内容,但我仍然无法弄清楚我的错误在哪里...... 根据 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 -----------------------------8disposition:-data; 名称=“消息”
Lombardi 喜欢 stackoverflow -----------------8cf5b7942cad9d0 Content-Disposition: form-data; 名称=“来源”;文件名="~\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