facebook c# sdk: Add/edit event with newlines in description

醉酒当歌 提交于 2019-12-20 04:06:25

问题


I am trying to edit events on facebook pages. The SDK works fine, but I have one problem.

Whenever there are newlines in the description (\r\n), they get posted to facebook. So I get event bodies like:

New event!\r\nWelcome to the event.

instead of

New event!
Welcome to the event.

looking in the result of a graph get to the event, I see that the newlines are double escaped, and looks like this:

...
"name": "TEST EVENT",
"description": "New event!\\r\\nWelcome to the event.",
"start_time": "2011-03-24T00:00:00"
...

I've tried any and all possible solutions I can think of, but to no avail. I've been reduced to replacing newlines with " * " just to separate sentences...

The text comes as a string in an object, and is transferred to a JsonObject.

JsonObject owner = new JsonObject { .... };
JsonObject evt = new JsonObject();
evt.Add("id", eventId);
evt.Add("owner", owner);
evt.Add("name", eventItem.Name);
evt.Add("updated_time", eventItem.Updated.ToString("o"));
evt.Add("description", eventItem.Description);
evt.Add("start_time", eventItem.StartDate.ToString("o"));
evt.Add("end_time", eventItem.EndDate.ToString("o"));
...
var fbc = new FacebookClient(internalObject.AccessToken);
result = (bool) fbc.Post(evt);

Any Ideas?

EDIT:

Workaround. I made a change to FacebookUtils.ToJsonQueryString. This works, but I guess it might be a bug in the SDK?

Looks like Facebook doesnt support standard Json escaping of newlines in event?

internal static string ToJsonQueryString(IDictionary<string, object> dictionary)
{
   ...

   jsonValue = jsonValue.Replace("\\n", "\n").Replace("\\r", "\r");

   if (!String.IsNullOrEmpty(jsonValue))
   {
      var encodedValue = UrlEncode(jsonValue);
      sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}", key, encodedValue);

   ...
   return sb.ToString();
}

This change makes \r\n in the returned querystring look like %0d%0a instead of %5cr%5cn, which is what facebook accepts.

Royan


回答1:


Fixed as of version SDK 5.0.8 - 25. march




回答2:


This works:

Use this: <center></center>

Instead of a br or a newline, etc. You can only do one in a row (ie. you can't increase the spacing).




回答3:


Is there any reason why you wouldn't use:

{
    "name": "TEST EVENT",
    "description": "New event!<br/>Welcome to the event.",
    "start_time": "2011-03-24T00:00:00"
} 

jsonlint validates it...



来源:https://stackoverflow.com/questions/5421890/facebook-c-sharp-sdk-add-edit-event-with-newlines-in-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!