RestClient: Can't add custom audience via API on Facebook

Question!

I'm trying to add custom audience on Facebook using Ruby restclient gem but there is something with the payload param that's not working. I'm setting "multipart" param to the payload for RestClient but this doesn't work neither. I get always a RestClient::BadRequest: 400 Bad Request

Here is an example of what I'm doing , any guest?

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

Building the request like this:

@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 Example that works:

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


Answers

The JSON class will do that:

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\"]]}"


So , the solution was to stringify FB's payload param .

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


You are almost there. Use getent for resolving UID to username:

getent passwd `grep "^Uid" /proc/$i/status |awk '{printf "%4s",$2}'`| cut -d: -f1
By : Misko


This video can help you solving your question :)
By: admin