【问题标题】:VB.NET and Facebook Photo Upload -- Getting 400 Bad RequestVB.NET 和 Facebook 照片上传——收到 400 错误请求
【发布时间】:2012-12-03 13:08:17
【问题描述】:

我已经尝试了从编写自己的标头到转换 C# 示例的所有方法,但上传照片时仍然无法通过 400 Bad Request 错误。

我已添加所有可能的权限,并且我的令牌是正确的。

我可以将状态更新发布到我的提要中,但我无法上传图片。这是我尝试过的两种不同方法,都给了我 400 Bad Request...

1

    Dim myReq As HttpWebRequest
    Dim myRes As HttpWebResponse
    Dim encoding As New System.Text.ASCIIEncoding()
    Dim postData As String
    Dim data() As Byte
    Dim sr As StreamReader
    Dim imagedata As String

    imagedata = File.OpenText("C:\ebay00042-1.jpg").ReadToEnd()

    postData += "access_token=MY_TOKEN_HERE_29ZB51pPizthxX5lhmst3MZC7hYXQhW8ZB8e7sVVLzEaN8ZCZAzAgrzk1pisw3ZCtK5lwMMTZBUhe07xTsQvfeHosA1GFUAZDZD&message=this is a test123&source=" & imagedata 'File.ReadAllBytes(photoPath)
    data = encoding.GetBytes(postData)

    myReq = WebRequest.Create("https://graph.facebook.com/380406275386560/photos")
    DirectCast(myReq, System.Net.HttpWebRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
    myReq.Method = "POST"
    myReq.ContentType = "application/x-www-form-urlencoded"
    myReq.ContentLength = data.Length
    Dim myStream As Stream = myReq.GetRequestStream
    myStream.Write(data, 0, data.Length)
    myStream.Close()
    myRes = myReq.GetResponse
    sr = New StreamReader(myRes.GetResponseStream)
    Dim strHTML As String = sr.ReadToEnd

2,尝试创建自己的标题..

    Dim myReq As HttpWebRequest
    Dim myRes As HttpWebResponse
    Dim encoding As New System.Text.ASCIIEncoding()
    Dim data() As Byte
    Dim sr As StreamReader
    Dim boundary As String = "----------" + DateTime.Now.Ticks.ToString("x")


    Dim sb As StringBuilder = New StringBuilder("")
    sb.Append("----------").Append(boundary).Append("\r\n")
    sb.Append("Content-Disposition: form-data; name=""access_token""").Append("\r\n")
    sb.Append("\r\n")
    sb.Append("MY_TOKEN_HERE_MZC7hYXQhW8ZB8e7sVVLzEaN8ZCZAzAgrzk1pisw3ZCtK5lwMMTZBUhe07xTsQvfeHosA1GFUAZDZD").Append("\r\n")

    sb.Append("----------").Append(boundary).Append("\r\n")
    sb.Append("Content-Disposition: form-data; name=""message""").Append("\r\n")
    sb.Append("\r\n")
    sb.Append("Testttt").Append("\r\n")

    sb.Append("----------").Append(boundary)
    sb.Append("Content-Disposition: file; name=""source"" filename=""ebay00042-1.jpg""").Append("\r\n")
    sb.Append("Content-Type: image/jpeg).Append(\r\n")
    'sb.Append("Content-Transfer-Encoding: binary").Append("\r\n")
    sb.Append("\r\n")
    sb.Append(File.OpenText("C:\ebay00042-1.jpg").ReadToEnd()).Append("\r\n")

    sb.Append("----------").Append(boundary).Append("----------").Append("\r\n")


    'txtCaption.Text = sb.ToString
    data = encoding.GetBytes(sb.ToString)


    myReq = WebRequest.Create("https://graph.facebook.com/380406275386560/photos")
    DirectCast(myReq, System.Net.HttpWebRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
    myReq.Method = "POST"
    myReq.ContentType = "multipart/form-data; boundary=" + boundary
    'myReq.ContentType = "application/x-www-form-urlencoded"
    myReq.ContentLength = data.Length
    Dim myStream As Stream = myReq.GetRequestStream
    myStream.Write(data, 0, data.Length)
    myStream.Close()
    myRes = myReq.GetResponse
    sr = New StreamReader(myRes.GetResponseStream)
    Dim strHTML As String = sr.ReadToEnd

任何帮助将不胜感激!

【问题讨论】:

    标签: vb.net facebook image upload photo


    【解决方案1】:

    我对 Facebook 的 API 一无所知,但如果我是你,我会好好看看 http://csharpsdk.org/

    如果我必须根据您提供的信息发送一张图片,它会是这样的:

    ' Request URL, image file to send, token and result HTML buffer
    Dim reqUrl As String = "https://graph.facebook.com/380406275386560/photos"
    Dim imageData As Byte() = File.ReadAllBytes("C:\ebay00042-1.jpg")
    Dim token As String = "MY_TOKEN_HERE"
    Dim strHtml As String = ""
    ' Request
    Dim request As WebRequest = WebRequest.Create(reqUrl)
    request.Headers.Add("access_token", token)
    request.Method = "POST"
    ' set *correct* content type
    request.ContentType = "image/jpeg"
    ' write image data to request stream
    Using str = request.GetRequestStream()
        str.Write(imageData, 0, imageData.Length)
    End Using
    ' response
    Dim response As WebResponse = request.GetResponse()
    ' HTTP Status
    Dim status As Integer = CType(response, HttpWebResponse).StatusCode
    If status = 200 Then
        ' success
        Using reader As New StreamReader(response.GetResponseStream())
            strHtml = reader.ReadToEnd()
        End Using
    Else
        ' oops
    End If
    

    希望对您有所帮助。

    【讨论】:

    • 我已经更新到 VS 2012,现在正在使用 Facebook c# sdk(在 vb.net 中).. 工作得更好。出于某种原因,我无法让它在 VS 2010 中运行。浪费了一整天的时间来尝试重做轮子:)
    猜你喜欢
    • 2021-03-08
    • 1970-01-01
    • 2011-10-13
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多