facebook-csharp-sdk/facebook-csharp-sdk — Issue #70: POST to graph + Json Encoding doesn't do what it "should"
Author: ntotten
Created: 2012-02-06 20:34:28+00:00
State: open
Labels: Bug

I'm using the ads api through graph. Just transferred over. We build up an elaborate set of ExpandoObjects to post the ad. One of the sub-ExpandoObjects is called "creative" and it has the creative. The JSON encoding that's happening in here seems to be brain damaged about escaping double quotes. Here's a part of the posted form grabbed from the stream. Note the "body" text has double quotes in it and whatever JSON encoding is happening inside the FacebookSDK seems to be falling down here:

Content-Disposition: form-data; name="creative"

{"title":"Put your quarters up!","body":"Please "check" out the classics at the coinop.org museum.","image_file":"@image_7036.png","type":27,"story_id":"266129693418752_286263474731831","object_id":266129693418752,"name":"Dynamic 1_polybius.aspx_Arcade Games"}
--8ce614f4e9a4932

this is the function we have to do the post:

public static dynamic GraphPost(string url, IDictionary postData)
{
var app = new Facebook.FacebookClient();
dynamic result = app.Post(url, postData);
return result;
}

the url is:

https://graph.facebook.com/act___________/adgroups?access_token=______

So it has the proper token, etc.

Any ideas on why that's happening? I don't want to write checks for double quote handling if this is a bug in the FacebookSDK, which at the moment I think is true.

thanks

File Attachments
No files are attached

Comments
prabirshrestha wrote Nov 10 2011 at 9:18 PM [x]
seems like it is a bug in SimpleJson.EscapeToJavascriptString. I have already patched it and pushed the changes.

could you download the latest version and give it a try. http://facebooksdk.codeplex.com/SourceControl/changeset/changes/3923a193ca45
let us know if it works as expected.

prabirshrestha wrote Nov 10 2011 at 8:53 PM [x]
bug confirmed

Minimalist360 wrote Nov 10 2011 at 8:11 PM [x]
PS the media gets uploaded fine with the quote hack in, replacing " with \" inside adtitle and adcopy. looks correct on the wire, and facebook accepts it fine.

Minimalist360 wrote Nov 10 2011 at 8:09 PM [x]
sure, but the double quote error is jsut in a regular property:

{"title":"Put your quarters up!","body":"Please "check" out the classics at the coinop.org museum."

the quotes around "check" are not escaped and this results in invalid json. I have a manual quote escape hac kin here now.
dynamic adgroup_specs= new ExpandoObject();

dynamic creative = new ExpandoObject();
creative.title = adtitle;
creative.body = adbody;

if (ad.FbCampaignUploadInfo.Image != null && ad.FbCampaignUploadInfo.Image.ImageId != 0)
{
creative.image_file = "@image_" + ad.FbCampaignUploadInfo.Image.ImageId + ".png";
var fmo = new Facebook.FacebookMediaObject { FileName = adgroup_specs.creative.image_file, ContentType = "image/png" };
fmo.SetValue(adg_dc.Images.Where(r => r.ImageId == ad.FbCampaignUploadInfo.Image.ImageId).First().ImageData);
((IDictionary)adgroup_specs).Add(fmo.FileName, fmo);
}

adgroup_specs.creative = creative;

The adgroup_specs has other properties etc added in here, then goes through some sub-methods that do things like create and append a token to the url, and then gets passed in here as postdata:

public static dynamic GraphPost(string url, IDictionary postData)
{
var app = new Facebook.FacebookClient();
dynamic result = app.Post(url, postData);
return result;
}

The json I originally posted was grabbed from the wire via fiddler2 and it errors out at facebook. I've added code to escape double quotes manually in the interim.

prabirshrestha wrote Oct 26 2011 at 3:51 AM [x]
could you paste the full code at how you are creating the parameters with the ExpandoObject.
It most likely you are setting the FacebookMediaObject in the wrong way.

Migrated from: http://facebooksdk.codeplex.com/workitem/5915