Facebook 批量请求
我正在尝试构建一个 VB.net 应用程序,该应用程序将通过 Graph API 批量请求方法从 facebook 检索各种用户。我的问题似乎是在执行实际请求时。当我尝试检索响应时,我从服务器返回 400 错误,指出“错误请求”。我不确定这是否意味着我的请求正文形成得不好,或者我是否在代码中附加了错误的内容。
这是我的请求代码:
Dim sURL As String
sURL = "https://graph.facebook.com"
Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
wrGETURL.Method = "POST"
wrGETURL.Proxy = WebRequest.DefaultWebProxy
Dim reqStream As IO.Stream = wrGETURL.GetRequestStream()
Dim access_token As String = "..." ' Access token removed, but it is valid
Dim jsonReq As String = """access_token"": """ + access_token + """"
jsonReq = jsonReq + """batch"": ["
AddBatchRequest(jsonReq, "[email protected]")
AddBatchRequest(jsonReq, "[email protected]")
jsonReq = jsonReq + "]"
Dim enc As New System.Text.UTF8Encoding()
Dim jsonBytes As Byte() = enc.GetBytes(jsonReq)
reqStream.Write(jsonBytes, 0, jsonBytes.Length)
reqStream.Close()
Dim oLabel As Label = output_label
Dim objStream As Stream
Try
objStream = wrGETURL.GetResponse.GetResponseStream() '<<<<Crashes here
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Dim ostr As String = ""
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
ostr = ostr + sLine
End If
Loop
oLabel.Text = ostr
Catch ex As Exception
oLabel.Text = ex.Message
End Try
Private Sub AddBatchRequest(ByRef json As String, ByVal email As String)
If json.EndsWith("}") Then
json = json + ","
End If
json = json + "{""method: ""GET"", ""relative_url"": ""search?q=" + email + "&type=user""}"
End Sub
我已经尝试了很多关于如何形成字符串主体的变体,但它们都不起作用,这让我相信我没有正确添加主体。
I'm attempting to build a VB.net application that will retrieve various users from facebook via their Graph API batch requests method. My problem seems to be in performing the actual request. When I attempt to retrieve the response, I get a 400 error back from the server saying "Bad Request". I'm not sure if that means I am forming the body of the request poorly, or if I am attaching it wrong in the code.
Here is my request code:
Dim sURL As String
sURL = "https://graph.facebook.com"
Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
wrGETURL.Method = "POST"
wrGETURL.Proxy = WebRequest.DefaultWebProxy
Dim reqStream As IO.Stream = wrGETURL.GetRequestStream()
Dim access_token As String = "..." ' Access token removed, but it is valid
Dim jsonReq As String = """access_token"": """ + access_token + """"
jsonReq = jsonReq + """batch"": ["
AddBatchRequest(jsonReq, "[email protected]")
AddBatchRequest(jsonReq, "[email protected]")
jsonReq = jsonReq + "]"
Dim enc As New System.Text.UTF8Encoding()
Dim jsonBytes As Byte() = enc.GetBytes(jsonReq)
reqStream.Write(jsonBytes, 0, jsonBytes.Length)
reqStream.Close()
Dim oLabel As Label = output_label
Dim objStream As Stream
Try
objStream = wrGETURL.GetResponse.GetResponseStream() '<<<<Crashes here
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Dim ostr As String = ""
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
ostr = ostr + sLine
End If
Loop
oLabel.Text = ostr
Catch ex As Exception
oLabel.Text = ex.Message
End Try
Private Sub AddBatchRequest(ByRef json As String, ByVal email As String)
If json.EndsWith("}") Then
json = json + ","
End If
json = json + "{""method: ""GET"", ""relative_url"": ""search?q=" + email + "&type=user""}"
End Sub
I've tried a ton of variations on how to form the string body, but none of them are working, leading me to believe that I am not adding the body correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是使用 Facebook SDK,它支持 批量请求。 你会做类似的事情:
我认为你的问题是 jsonReq 不是格式正确的 POST 身体。将该值粘贴到此处,我相信有人可以具体指出问题所在。我没有安装VB.NET。
The easiest thing to do would be to use the Facebook SDK which supports batch requests. You would do something like:
I think your problem though is that the jsonReq is not a properly formatted POST body. Paste that value here and I'm sure someone can point out specifically what is wrong. I don't have VB.NET installed.