如何对 & 进行编码批量请求的符号?

发布于 2024-12-06 01:42:11 字数 1263 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-12-13 01:42:11

在实际尝试了更多之后,我成功地重现了相同的行为,即使仔细检查并仔细检查我是否正确遵循了 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.

囚你心 2024-12-13 01:42:11

不要使用字符串文字来构造 json。使用 to_json,如下所示。 (另外,顺便说一句,不要在多行中使用 {} 表示法,请使用 do/end)。

queries = []
email_array.each do |email|
  queries << {:method => 'GET', :relative_url => "search?q=#{email}&type=user"}
end

route = "https://graph.facebook.com/?access_token=#{token}&batch=#{URI.encode(queries.to_json)}"
res = HTTParty.post(route)

另外,您可以使用 Array#map 来简化代码,如下所示:

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)

编辑:下面是我在编辑问题之前的原始答案,供参考。

尝试正确的 url 编码整个参数:

https://graph.facebook.com/?access_token=ACCESS_TOKEN&batch=[%7B%22method%22:%20%22GET%22,%20%22relative_url%22:%20%[email protected]&type=user%22%7D]

实际上,您可以使用 uri 库中的 URI.encode 来执行此操作。示例:

irb(main):001:0> require 'uri'
=> true
irb(main):002:0> URI.encode('[{"method": "GET", "relative_url": "[email protected]&type=user"}]')
=> "[%7B%22method%22:%20%22GET%22,%20%22relative_url%22:%20%[email protected]&type=user%22%7D]"

或者更好的是,首先使用 to_json 创建 json 字符串。例子:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'json'
=> true
irb(main):003:0> require 'uri'
=> true
irb(main):004:0> URI.encode([{:method => 'GET', :relative_url => '[email protected]&type=user'}].to_json)
=> "[%7B%22method%22:%22GET%22,%22relative_url%22:%[email protected]&type=user%22%7D]"

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, use do/end).

queries = []
email_array.each do |email|
  queries << {:method => 'GET', :relative_url => "search?q=#{email}&type=user"}
end

route = "https://graph.facebook.com/?access_token=#{token}&batch=#{URI.encode(queries.to_json)}"
res = HTTParty.post(route)

Also, you can use Array#map to simply the code, like this:

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)

EDIT: below is my original answer before the question was edited, for reference.

Try properly url encoding the whole parameter:

https://graph.facebook.com/?access_token=ACCESS_TOKEN&batch=[%7B%22method%22:%20%22GET%22,%20%22relative_url%22:%20%[email protected]&type=user%22%7D]

In practice, you'd use URI.encode from the uri library to do this. Example:

irb(main):001:0> require 'uri'
=> true
irb(main):002:0> URI.encode('[{"method": "GET", "relative_url": "[email protected]&type=user"}]')
=> "[%7B%22method%22:%20%22GET%22,%20%22relative_url%22:%20%[email protected]&type=user%22%7D]"

Or even better, use to_json to create your json string in the first place. Example:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'json'
=> true
irb(main):003:0> require 'uri'
=> true
irb(main):004:0> URI.encode([{:method => 'GET', :relative_url => '[email protected]&type=user'}].to_json)
=> "[%7B%22method%22:%22GET%22,%22relative_url%22:%[email protected]&type=user%22%7D]"
黎夕旧梦 2024-12-13 01:42:11

如果这对任何人有帮助,当我的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文