Facebook Graph API and python facebook-sdk upload multiple files

816 Views Asked by At

I'm trying to post to the Page feed with multiple images. I'm following Doc's Facebook Graph API and Doc's facebook-sdk for python. Only the message is published, without the image.

token = "my_token"

graph = facebook.GraphAPI(access_token=token, version="3.0")
photo_id = graph.put_photo(image=open('favicon.png', 'rb'), published=False)
print('PHOTO ID ', photo_id.get('id', ''))
post = graph.put_object(parent_object="page_id", connection_name="feed", message="Message with images upload!", attachments=[{'media_fbid': photo_id.get('id', '')}])
print('POST ID', post.get('id', ''))

Image and post IDs are returned without errors. However, the image is not published along with the message. I'm using attachments parameter, maybe it's another. Any idea?

1

There are 1 best solutions below

0
Dev Python On

I found the answer:

You should use json.dumps and attached_media as parameter:

import json

graph.put_object(parent_object="page_id", connection_name="feed", message="Message with multiple files!", attached_media=json.dumps([{'media_fbid': str(photo_id.get('id', ''))}]))