Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python POST to Meta Conversion API missing parameter

hoping this is a easy question and thank you for the help.

My Goal: Send some data from Hubspot Workflow to Meta/Facebook's Conversion API.

My Problem: When I try to send data over I receive the following error:

{"error":{"message":"(#100) The parameter data is required","type":"OAuthException","code":100,"fbtrace_id":"AajsbCdtXpN5mhcKQaTylmC"}}

My Code:


  url = f'https://graph.facebook.com/v13.0/data_ommitted/events?access_token={facebookkey}&test_event_code=TEST79983'
  
  headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
  
  myData = {
    "action_source" : "website",
    "event_name" : "Lead",
    "event_time" : unix_timestamp,
    "user_data": {
      "em": [email],
      "fn": [firstname],
      "ln": [lastname],
      "zp": [zipcode],
    } 
  }
  
  print(myData)
  try:
      x = requests.post(url, data=myData, headers=headers)
      print(x.text)
  except Exception as e: 
    print(e)
  finally:
    print('Succesfully sent.')

What I've Tried: I tried the following, which got me a little closer:

 x = requests.post(url, data={"data": [myData]}, headers=headers)
      print(x.text)

{"error":{"message":"(#100) param data must be an array."

Example Data:

print(myData)
{'action_source': 'website', 'event_name': 'Lead', 'event_time': 1652901832, 'user_data': {'em': ['7c288df52b285801c4fe184d257d4ba521ece9dc2d74fddef21fde5b763fa693'], 'fn': ['9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'], 'ln': ['9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'], 'zp': ['dc937b59892604f5a86ac96936cd7ff09e25f18ae6b758e8014a24c7fa039e91']}}

What Facebook Expects:

{
  "data": [
    {
      "event_name": "Lead",
      "event_time": 1652898537,
      "action_source": "website",
      "user_data": {
        "em": [
          "7c288df52b285801c4fe184d257d4ba521ece9dc2d74fddef21fde5b763fa693"
        ],
        "zp": [
          "dc937b59892604f5a86ac96936cd7ff09e25f18ae6b758e8014a24c7fa039e91"
        ],
        "fn": [
          "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
        ],
        "ln": [
          "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
        ]
      }
    }
  ]
}

Thank you for help!

like image 847
sylargaf Avatar asked Nov 04 '25 10:11

sylargaf


1 Answers

I figured it out! :) This is what you need to send:

      x = requests.post(url, data=json.dumps({"data":[myData]}), headers=headers)

like image 108
sylargaf Avatar answered Nov 07 '25 06:11

sylargaf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!