python - Python POST 到 Meta Conversion API 缺少参数

希望这是一个简单的问题,并感谢您的帮助。

我的目标:将一些数据从 Hubspot 工作流发送到 Meta/Facebook 的转换 API。

我的问题:当我尝试发送数据时,我收到以下错误:

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

我的代码:

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.')

我尝试了什么:我尝试了以下方法,这让我更接近了:

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

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

示例数据:

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

Facebook 的期望:

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

谢谢你的帮助!

回答1

我想到了! :) 这是您需要发送的内容:

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

相似文章