简体   繁体   English

Facebook Graph API Linebreak

[英]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. 我知道有很多关于此问题的问题 - 但我想在通过Graph API发布的Facebook墙上帖中添加换行符。 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. 我已经看到了各种建议使用<center></center>的答案,但这对我不起作用 - 它只是在帖子中打印' <center></center> '。

To be specific, I'm using Ruby and the Koala gem. 具体来说,我使用的是Ruby和Koala宝石。 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. 更新:我已经向Koala gem提交了一个补丁,允许设置:no_encoding选项。 If/when this patch is accepted, it will allow for linebreaks if that options is set to true. 如果/当此补丁被接受时,如果该选项设置为true,则允许换行。 For now, folks with this issue can grab my fork of the gem: https://github.com/ideaoforder/koala 现在,有这个问题的人可以抓住我的宝石分支: 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. 请求方法设置为:url_encode,它将params作为url编码的表单数据发送。 The params have to be sent as regular data, not form data (like using the -d flag instead of the -f flag in cURL). params必须作为常规数据发送,而不是表单数据(比如在cURL中使用-d标志而不是-f标志)。 We accomplish this by sending data as part of the query string instead of encoded params. 我们通过将数据作为查询字符串的一部分而不是编码的params来完成此操作。

What facebook actually accepts in posts seems to change from time to time, and isn't documented very well. Facebook在帖子中实际接受的内容似乎时有变化,并且没有很好地记录。 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 第1行
line 2 第2行

The important part is to know what's getting url-encoded where. 重要的是要知道什么是url编码在哪里。 The message parameter in my API request contains %0D%0A , which is the equivalent of an escape sequence of \\r\\n ( see here ). 我的API请求中的message参数包含%0D%0A ,它等同于\\r\\n的转义序列( 请参见此处 )。 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. 如果您使用的Koala gem是对您提供的输入进行url编码,那么传递带有\\r\\n转义码的字符串应该是您所需要的。

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命令:

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). 像往常一样用你自己的替换wall id和访问令牌参数(让你的Ruby程序打印出来自Facebook的access_token )。 You should see a string of JSON as output: 您应该看到一个JSON字符串作为输出:

{"id":"wallid_postid"}

Where wallid and postid are numbers. wallidpostid是数字。 If instead you get an error, it's possible there is a permission problem with Facebook or your application. 如果您收到错误,Facebook或您的应用程序可能存在权限问题。

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. 权限 :如果您要发布该页面 ,您的应用必须具有来自页面管理员的manage_pages权限,并且用户必须当前在请求时登录到您的应用和Facebook,除非您的应用还请求了offline_access权限。 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. Facebook页面也可以拥有自己的笔记。 Simply add ?sk=notes to your account's or Page's URL, then you will be seeing Facebook's Notes application. 只需将?sk = notes添加到您的帐户或Page的URL中,您就会看到Facebook的Notes应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM