开发者

Facebook Graph API Linebreak

I know there are a slew of questions about this already--but I'd like to add linebreaks to Facebook wall posts that are being posted via the Graph API. I've seen a variety of answers suggesting using <center></center>, but that doesn't work for me--it just prints '<center></center>' in the post.

To be specific, I'm using Ruby and the Koala gem. I am attempting to post to a user's page. If I post as a link and try the center hack in the description of the post, it works as expected--but not in the message, where I actually need it.

UPDATE: I've submitted a patch to the Koala gem that allows a :no_encoding option to be set. If/when this patch is accepted, it will allow for linebreaks if that options is set to true. For now, folks with this issue can grab my fork of开发者_StackOverflow the gem: https://github.com/ideaoforder/koala

For those who are curious, the issue was with Faraday. The request method was set to :url_encode which sends the params as url-encoded form data. The params have to be sent as regular data, not form data (like using the -d flag instead of the -f flag in cURL). We accomplish this by sending data as part of the query string instead of encoded params.


What facebook actually accepts in posts seems to change from time to time, and isn't documented very well. Given that, I was able to create a wall post with line breaks in the message by making the following request:

POST https://graph.facebook.com/me/feed?access_token=<token>&message=line+1%0D%0Aline+2

creating the following post on my wall:

line 1
line 2

The important part is to know what's getting url-encoded where. The message parameter in my API request contains %0D%0A, which is the equivalent of an escape sequence of \r\n (see here). If the Koala gem you're using is url-encoding the input you give it, then passing a string with the \r\n escape codes should be all you need.

Update: It may be useful to try making the post manually using a command-line program. See if you can get it working with this curl command:

curl 'https://graph.facebook.com/<wall id>?access_token=<access_token>' -d 'message=this+is+line+1%0D%0Athis+is+line+2'

As usual substitute the wall id and access token parameters with your own (have your Ruby program print out the access_token it gets from Facebook). You should see a string of JSON as output:

{"id":"wallid_postid"}

Where wallid and postid are numbers. If instead you get an error, it's possible there is a permission problem with Facebook or your application.

Permissions: Your app must have the manage_pages permission from an administrator of a page if you're posting as that page, and the user must be currently logged in to your app and facebook when the request is made, unless your app has also requested the offline_access permission. If the user is not logged in you will get this response:

{"error": {
    "type":"GraphMethodException",
    "message":"Unsupported post request."
}}


I also struggled a while with the line-break on statuses. I found that rather than struggling with inserting line-breaks in a status, using notes would give a lot flexibility if one's desire could be also met with this approach. A Facebook Page can also possess own notes. Simply add ?sk=notes to your account's or Page's URL, then you will be seeing Facebook's Notes application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜