如何对 & 进行编码批量请求的符号?
我有一个 Facebook 批量请求,如下所示:
https://graph .facebook.com/?access_token=ACCESS_TOKEN&batch=[{"method": "GET", "relative_url": "[email protected]&type=user"}]
通过网络发送此返回:
{"error"=>0, "error_description"=>"batch parameter must be a JSON array"}
如果我删除&type=user
,它工作正常(发回一个空数据数组)。我绝对确定 Facebook 不会解析 &字符正确。我在网上读到我可以尝试对 & 进行编码符号到 %26,但是使用该替换似乎会查询“[email protected] ]%26type=用户”。如果你颠倒参数的顺序,你就会明白我的意思。
有什么想法可以让 Facebook 上的批量请求解析器识别 & 吗?符号而不提交永远不会被修复的错误报告?
编辑:
我正在使用 URI.encode。这是确切的代码:
queries = email_array.map { |email| { :method => "GET", :relative_url => "search?q=#{email}&type=user" } }
route = "https://graph.facebook.com/?access_token=#{token}&batch=#{URI.encode(queries.to_json)}"
res = HTTParty.post(route)
I have a Facebook batch request that looks like this:
https://graph.facebook.com/?access_token=ACCESS_TOKEN&batch=[{"method": "GET", "relative_url": "[email protected]&type=user"}]
Sending this across the wire returns:
{"error"=>0, "error_description"=>"batch parameter must be a JSON array"}
If I remove the &type=user
, it works fine (sends back an empty data array). I am absolutely certain that Facebook is not parsing the & character correctly. I read online somewhere that I could try encoding the & symbol to %26, however using that replacement seems to instead do a query for "[email protected]%26type=user". If you reverse the order of the parameters, you will see what I mean.
Any ideas how I can get the batch request parser on Facebook to recognize the & symbol without filing a bug report that will never be fixed?
EDIT:
I am using URI.encode. Here is the exact code:
queries = email_array.map { |email| { :method => "GET", :relative_url => "search?q=#{email}&type=user" } }
route = "https://graph.facebook.com/?access_token=#{token}&batch=#{URI.encode(queries.to_json)}"
res = HTTParty.post(route)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在实际尝试了更多之后,我成功地重现了相同的行为,即使仔细检查并仔细检查我是否正确遵循了 api 规范。这看起来像是 facebook 批处理方法中的一个错误——它无法正确理解参数值中的&符号。
After actually playing around with this some more, I managed to reproduce the same behavior, even with a careful check and double-check that I was following the api specs correctly. This looks like a bug in facebook's batch method -- it doesn't understand ampersands in param values correctly.
不要使用字符串文字来构造 json。使用
to_json
,如下所示。 (另外,顺便说一句,不要在多行中使用{}
表示法,请使用do
/end
)。另外,您可以使用 Array#map 来简化代码,如下所示:
编辑:下面是我在编辑问题之前的原始答案,供参考。
尝试正确的 url 编码整个参数:
实际上,您可以使用
uri
库中的URI.encode
来执行此操作。示例:或者更好的是,首先使用
to_json
创建 json 字符串。例子:Don't use a string literal to construct the json. Use
to_json
, like below. (Also, as an aside, don't use{}
notation across more than one line, usedo
/end
).Also, you can use
Array#map
to simply the code, like this:EDIT: below is my original answer before the question was edited, for reference.
Try properly url encoding the whole parameter:
In practice, you'd use
URI.encode
from theuri
library to do this. Example:Or even better, use
to_json
to create your json string in the first place. Example:如果这对任何人有帮助,当我的 AdSet 批量更新由于存在“&”而失败时兴趣名称之一:
{u'id': u'6003531450398', u'name': u'Dolce & Gabbana'}
我了解到名称可以是任何内容,只要 id 正确,FB 就会填充名称本身。
If this helps anyone, when my AdSet batch update failed because there was an "&" in one of the interests name:
{u'id': u'6003531450398', u'name': u'Dolce & Gabbana'}
I learned that the name can be anything, and as long as the id is correct, FB will populate the name itself.