2012-09-08 36 views
3

我想使用asp.net發佈圖像到facebook grap api, 我知道有一個很棒的帖子在這裏 Posting image from .NET to Facebook wall using the Graph API,因爲我需要在vb.net我只是轉換那邊描述的代碼的代碼,但同時嘗試後,他的圖片至Facebook我收到以下錯誤:將圖像從asp.net(VB.net)發佈到臉譜圖api(錯誤400錯誤請求)

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

對我來說是一個利特爾位很難調試這個錯誤,因爲這是來自臉部的唯一請求,因此我不知道錯誤是因爲我的表單郵政編碼沒有正確生成,或者錯誤是在我試圖發送的圖像中。

我正在閱讀很多關於http表單的帖子,但我仍然無法弄清楚我的錯誤在哪裏......根據Example provided by facebook我相信我確實有所有的requiered參數。

代碼中的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:form-data;名稱= 「ACCESS_TOKEN」

DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2bUKAfbTE54aBB5vdHFJaecGHPpGrLCrd5bEZWxlXvVKej0ApDbjEzjki8xzvl28etjRxH1LzcJP314RO5HJDbNbZJ ----------------------------- 8cf5b7942cad9d0 內容 - 處置:表單數據; name =「message」

Lombardi like stackoverflow ----------------------------- 8cf5b7942cad9d0 Content-Disposition:form-數據; NAME = 「源」;文件名=「〜\ img \ taco.jpeg」 內容類型: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

什麼攪拌機男人...問題是Facebook的令牌自身...其實我是加密令牌看來我的加密功能有某種解密時遇到的問題。無論如何,我會把代碼放在這裏,希望別人能找到有用的東西。

+0

您應該看過_HTTP Response Body_--通常在那裏有一個錯誤信息,可以解釋問題所在......但只要您的應用程序只關心HTTP Response Headers,您就會被矇在鼓裏。 – CBroe