RestClient:无法通过Facebook在Facebook上添加自定义受众群体

时间:2016-09-28 20:11:09

标签: ruby-on-rails ruby facebook rest-client facebook-marketing-api

我正在尝试使用Ruby restclient gem 在Facebook上添加自定义受众群体,但有效负载参数无法正常工作。 我正在为RestClient的有效负载设置“multipart”参数,但这两者都不起作用。我总是RestClient::BadRequest: 400 Bad Request

以下是我正在做的一个例子,任何一位客人?

payload =  {:payload => 
   {:schema=>["FN", "LN", "EMAIL"],
    :data=>[
     ["31c88cbf2ceeb43fce68f027c10dd71b053672ce771fa5e4fc22422e633c6f11",
      "3565d65adb5cc2043e09097ee55ad430333a2a9a08a3a2b82c158b160ff12454",
      "d66746d2a1a6d488601e5603b8e9540c03e7e8063ae8aa921a3860007f478761"],
     ["3f03df52a32aafde65e6ee94b351970e0fa1481337b6b02f5f607d3357c1c368",
      "2ef9487065c3616d7c108c4b1c81e1b3afeee48f0373f7d5f5220bbc38db8ef5",
      "e4e91c99e2ae79c614e35de6cb429c32510dc2ecbf11c2a6e45a5872693542e6"],
     ["2de75eeda225156c96688715f0c2f86276dc0f10453dad716b9419729c46c5b8",
      "68c91edc2305b00a20a7febc616d2925e58bdbdf27fdcef8f84a1d1d8f37b3af",
      "a63ef4bd9dd5985f46674b4fb7f98b63c7144aae9b243281d01a66ad7659247e"]]
    },
   :access_token => "<ACCESS_TOKEN>",
   :multipart => true }
url = "https://graph.facebook.com/v2.7/<AUDIENCE_ID>/users"   

request = RestClient::Request.new(
:method => :post,
:url => url,
:payload=>payload)
response = request.execute

建立这样的请求:

@payload="--474291\r\nContent-Disposition: form-data; name=\"payload[schema][]\"\r\n\r\nFN\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[schema][]\"\r\n\r\nLN\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[schema][]\"\r\n\r\nEMAIL\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n31c88cbf2ceeb43fce68f027c10dd71b053672ce771fa5e4fc22422e633c6f11\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n3565d65adb5cc2043e09097ee55ad430333a2a9a08a3a2b82c158b160ff12454\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\nd66746d2a1a6d488601e5603b8e9540c03e7e8063ae8aa921a3860007f478761\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n3f03df52a32aafde65e6ee94b351970e0fa1481337b6b02f5f607d3357c1c368\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n2ef9487065c3616d7c108c4b1c81e1b3afeee48f0373f7d5f5220bbc38db8ef5\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\ne4e91c99e2ae79c614e35de6cb429c32510dc2ecbf11c2a6e45a5872693542e6\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n2de75eeda225156c96688715f0c2f86276dc0f10453dad716b9419729c46c5b8\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\n68c91edc2305b00a20a7febc616d2925e58bdbdf27fdcef8f84a1d1d8f37b3af\r\n--474291\r\nContent-Disposition: form-data; name=\"payload[data][]\"\r\n\r\na63ef4bd9dd5985f46674b4fb7f98b63c7144aae9b243281d01a66ad7659247e\r\n--474291\r\nContent-Disposition: form-data; name=\"access_token\"\r\n\r\nXXXXXXXXXXXXXXXXXXXX\r\n--474291--\r\n"

CURL示例有效:

curl \
  -F 'payload={ 
    "schema": [ 
      "FN", 
      "LN", 
      "EMAIL" 
    ], 
    "data": [["31c88cbf2ceeb43fce68f027c10dd71b053672ce771fa5e4fc22422e633c6f11",
      "3565d65adb5cc2043e09097ee55ad430333a2a9a08a3a2b82c158b160ff12454",
      "d66746d2a1a6d488601e5603b8e9540c03e7e8063ae8aa921a3860007f478761"],
     ["3f03df52a32aafde65e6ee94b351970e0fa1481337b6b02f5f607d3357c1c368",
      "2ef9487065c3616d7c108c4b1c81e1b3afeee48f0373f7d5f5220bbc38db8ef5",
      "e4e91c99e2ae79c614e35de6cb429c32510dc2ecbf11c2a6e45a5872693542e6"],
     ["2de75eeda225156c96688715f0c2f86276dc0f10453dad716b9419729c46c5b8",
      "68c91edc2305b00a20a7febc616d2925e58bdbdf27fdcef8f84a1d1d8f37b3af",
      "a63ef4bd9dd5985f46674b4fb7f98b63c7144aae9b243281d01a66ad7659247e"]] 
  }' \
  -F 'access_token=XXXXX' \
  https://graph.facebook.com/v2.7/XXXX/users --trace-ascii /dev/stdout

2 个答案:

答案 0 :(得分:0)

因此,解决方案是将FB的有效负载参数字符串化。

payload =  {:payload => "{\"schema\":[\"FN\",\"LN\",\"EMAIL\"],\"data\":[[\"31c88cbf2ceeb43fce68f027c10dd71b053672ce771fa5e4fc22422e633c6f11\",\"3565d65adb5cc2043e09097ee55ad430333a2a9a08a3a2b82c158b160ff12454\",\"d66746d2a1a6d488601e5603b8e9540c03e7e8063ae8aa921a3860007f478761\"],[\"3f03df52a32aafde65e6ee94b351970e0fa1481337b6b02f5f607d3357c1c368\",\"2ef9487065c3616d7c108c4b1c81e1b3afeee48f0373f7d5f5220bbc38db8ef5\",\"e4e91c99e2ae79c614e35de6cb429c32510dc2ecbf11c2a6e45a5872693542e6\"],[\"2de75eeda225156c96688715f0c2f86276dc0f10453dad716b9419729c46c5b8\",\"68c91edc2305b00a20a7febc616d2925e58bdbdf27fdcef8f84a1d1d8f37b3af\",\"a63ef4bd9dd5985f46674b4fb7f98b63c7144aae9b243281d01a66ad7659247e\"]]}",
   :access_token => "XXXXXX",
   :multipart => true }
url = "https://graph.facebook.com/v2.7/XXXXXXX/users"
request = RestClient::Request.new(
 :method => :post, :url => url, :payload=>payload)
response = request.execute

答案 1 :(得分:0)

JSON类会这样做:

require 'json'

payload = {
  :payload => 
  {
    :schema => ["FN", "LN", "EMAIL"],
    :data => [
      ["foo", "bar", "baz"],
      ["foo", "bar", "baz"],
      ["foo", "bar", "baz"]
    ]
  },
  :access_token => "<ACCESS_TOKEN>",
  :multipart => true 
}

payload[:payload].to_json
# => "{\"schema\":[\"FN\",\"LN\",\"EMAIL\"],\"data\":[[\"foo\",\"bar\",\"baz\"],[\"foo\",\"bar\",\"baz\"],[\"foo\",\"bar\",\"baz\"]]}"