Reputation: 41
I am trying to post an image to facebook grap api using asp.net , I know that there is a great post about it here Posting image from .NET to Facebook wall using the Graph API since I need the code in vb.net I just converted the code described over there but while trying to post he picture to facebook I am getting the following error:
The remote server returned an error: (400) Bad Request.
For me is a litle bit hard to debug this error since this is the only request comming from face therefore I don't know if the error is because my form post code is not generated properly or the error is whithin the image I'm trying to send.
I was reading a lot about http form post but still I am not able to figure out where my error is ... acordingly to the Example provided by facebook I believe I do have all the requiered parameters.
Code in facebook example:
// 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>';
my form post generated code (before sending image)
-----------------------------8cf5b7942cad9d0 Content-Disposition: form-data; name="access_token"
DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2bUKAfbTE54aBB5vdHFJaecGHPpGrLCrd5bEZWxlXvVKej0ApDbjEzjki8xzvl28etjRxH1LzcJP314RO5HJDbNbZJ -----------------------------8cf5b7942cad9d0 Content-Disposition: form-data; name="message"
Lombardi likes stackoverflow -----------------------------8cf5b7942cad9d0 Content-Disposition: form-data; name="source"; filename="~\img\taco.jpeg" Content-Type: application/octet-stream
Any help will be highly appreciated .
And this is my full function code
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
Upvotes: 3
Views: 1999
Reputation: 41
What a blender man ... the problem was the facebook token itself... actually I am encrypting the token seems that my encryption functions are having some sort of issue while decrypting. Anyways I will leave the code over here hopefuly someone else will find useful.
Upvotes: 1