facebookcurlfacebook-graph-apifacebook-messenger-botmessenger

facebook messenger curl request returns <Response [400]>


I am new to the messenger API, I want to send a message using a curl post request, this is my code:

import requests

ACCESS_TOKEN = an Active access token

fb_url = "https://graph.facebook.com/v10.0/me/messages"
data = {
'recipient': '{"id":4098757906843152}',
"message": {
"text": "hello, world!"
},
"messaging_type": "MESSAGE_TAG",
"tag": "ACCOUNT_UPDATE"
}

params = {'access_token': ACCESS_TOKEN}
resp = requests.post(fb_url, params=params, data=data)
print(resp)

unfortunately, I got this message <Response [400]> any help would be appreciated


Solution

  • You need to change data to json. See https://stackoverflow.com/a/26344315/603756

    Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call

    import requests
    
    ACCESS_TOKEN = '<access_token>'
    
    fb_url = 'https://graph.facebook.com/v10.0/me/messages'
    data = {
        'recipient': '{"id":<psid>}',
        "message": {
        "text": "hello, world!"
        }
    }
    params = {'access_token': ACCESS_TOKEN}
    
    resp = requests.post(fb_url, json=data, params=params)
    print(resp)