0

I am trying to upload a file to Facebook send API via python requests. Curl to send API request is as follows

curl  \
  -F 'recipient={"id":"<PSID>"}' \
  -F 'message={"attachment":{"type":"<ASSET_TYPE>", "payload":{"is_reusable":true}}}' \
  -F 'filedata=@/tmp/shirt.png;type=image/png' \
  "https://graph.facebook.com/v9.0/me/messages?access_token=<PAGE_ACCESS_TOKEN>" 

I'm referring to this facebook documentation

I have tried the API using python requests

import requests
r = requests.get('https://homepages.cae.wisc.edu/~ece533/images/us092.pgm')
with open('temp.jpg', 'wb') as f:
   f.write(r.content)

files = {
    'recipient': (None, '{"id":" <USER_ID>"}'),
    'message': (None, '{"attachment":{"type":"image", "payload":{"is_reusable":true}}}'),
    'filedata': ('temp.jpg', open('temp.jpg', 'rb')),
}
url = "https://graph.facebook.com/v9.0/me/messages"
params = {"access_token":"<ACCESS_TOKEN>"}

response = requests.post(url=url,params=params, files=files)
print(response.content)

I'm getting response as follows

{"error":{"message":"(#100) Upload attachment failure.","type":"OAuthException","code":100,"error_subcode":2018047,"fbtrace_id":"Atv2YqLlb4ABkoVOW4sgnzd"}}

Following is the description of the error code

enter image description here

I can't figure out what I'm doing wrong

  • is it now possible to read direct messages of account that is using Send API app to analyze the DMs?? – oldboy May 08 '21 at 12:00

1 Answers1

0

Try adding ;type=image/png to temp.jpg at filedata key. This should look like:

'filedata': ('temp.jpg;type=image/png', open('temp.jpg', 'rb')),
Lucas Vazquez
  • 1,456
  • 16
  • 20