0

I'm trying to upload some stuff using Facebooks API here but I keep getting the error "This field must be a string.", and it seems like it is related to how I parse my "data" field (if I copy-paste it into their Graph API, it works fine).

If you don't know what the file to Facebooks API takes it is;

data - json array of data 
upload_tag  - string
access_token - token

right now I have my data in a pandas data-frame and I'm using the data = df.to_json(orient="records") to generate the json-string parsed in to the data field

data = df.to_json(orient="records")

file = {"access_token":token
upload_tag: "test",
data = data}

r = requests.post(f"https://graph.facebook.com/v11.0/{offline_event_id}/events",files = file)
#{"error":{"message":"(#100) This field must be a string.","type":"OAuthException","code":100,"fbtrace_id":"some_code"}}

I have tried different formations of the data-string but getting the same error (even when copy-pasting Facebooks example-string into my code).

I wonder if requests somehow format the files in a wrong way, when sending the POST-request? Looking at r.request.body (of facebooks example data) I get the following

Content-Disposition: form-data; name="data"; filename="data"

[{"match_keys": {"phone": ["HASH1", "HASH2"], "email": ["HASH3", "HASH4"]}, "currency": "USD", "value": 16, "event_name": "Purchase", "event_time": 1456870902}]

which indeeed does not seem to be a string

CutePoison
  • 4,679
  • 5
  • 28
  • 63

1 Answers1

0

I found this SO question which linked to this gist. Using the function files = multipartify(file) accepted the request. To be honest, I don't know why, but it worked.

CutePoison
  • 4,679
  • 5
  • 28
  • 63