Reputation: 467
So I'm trying to use the new "batches" functionality to the graph API, which is described here. I'm think the problem is with the way I'm using POST to submit the data, and I'm having difficulty debugging it. It could be a JSON issue, but I don't think so. Here's the c#
HttpWebRequest httpRequest =(HttpWebRequest)WebRequest.Create("https://graph.facebook.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(o.ToString());
httpRequest.ContentLength = bytedata.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
string APIData = reader.ReadToEnd();
JObject MyApiData = JObject.Parse(APIData);
And the variables "o" contains the following JSON:
{
"access_token": "[my real token]",
"batch": [
{
"method": "get",
"relative_url": "me"
},
{
"method": "get",
"relative_url": "me/friends"
}
]
}
Any ideas what I'm doing wrong? It actually outputs the facebook's developer website documentation....so I think that's a clue that it's messed up ;-)
Upvotes: 0
Views: 1368
Reputation: 80
Try this out: private void PostBatch(string _token) {
string p1 = "access_token=" + Server.UrlEncode(_token);
string p2 = "&batch=" + Server.UrlEncode(" [ { \"method\": \"get\", \"relative_url\": \"me\" }, { \"method\": \"get\", \"relative_url\": \"me/friends\" } ]");
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://graph.facebook.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytedata = Encoding.UTF8.GetBytes(p1 + p2);
httpRequest.ContentLength = bytedata.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
string APIData = reader.ReadToEnd();
Response.Write(APIData);
}
catch (Exception ex)
{ Response.Write(ex.Message.ToString() + "<br>"); }
// JObject MyApiData = JObject.Parse(APIData);
}
Upvotes: 3