pythonfacebookfacebook-graph-apiuploadphoto

Upload image to facebook using the Python API


I have searched the web far and wide for a still working example of uploading a photo to facebook through the Python API (Python for Facebook). Questions like this have been asked on stackoverflow before but non of the answers I have found work anymore.

What I got working is:

import facebook as fb

cfg = {
    "page_id"      : "my_page_id",
    "access_token" : "my_access_token"
    }

api = get_api(cfg)
msg = "Hello world!"
status = api.put_wall_post(msg) 

where I have defined the get_api(cfg) function as this

graph = fb.GraphAPI(cfg['access_token'], version='2.2')

# Get page token to post as the page. You can skip
# the following if you want to post as yourself.
resp = graph.get_object('me/accounts')
page_access_token = None
for page in resp['data']:
    if page['id'] == cfg['page_id']:
        page_access_token = page['access_token']
graph = fb.GraphAPI(page_access_token)
return graph

And this does indeed post a message to my page. However, if I instead want to upload an image everything goes wrong.

# Upload a profile photo for a Page.
api.put_photo(image=open("path_to/my_image.jpg",'rb').read(), message="Here's my image")

I get the dreaded GraphAPIError: (#324) Requires upload file for which non of the solutions on stackoverflow works for me. If I instead issue the following command

api.put_photo(image=open("path_to/my_image.jpg",'rb').read(), album_path=cfg['page_id'] + "/picture")

I get GraphAPIError: (#1) Could not fetch picture for which I haven't been able to find a solution either.

Could someone out there please point me in the right direction of provide me with a currently working example? It would be greatly appreciated, thanks !


Solution

  • A 324 Facebook error can result from a few things depending on how the photo upload call was made

    A raw cURL call looks like

    curl -F 'source=@my_image.jpg' 'https://graph.facebook.com/me/photos?access_token=YOUR_TOKEN'

    As long as the above calls works, you can be sure the photo agrees with Facebook servers.

    An example of how a 324 error can occur

    touch meow.jpg

    curl -F 'source=@meow.jpg' 'https://graph.facebook.com/me/photos?access_token=YOUR_TOKEN'

    This can also occur for corrupted image files as you have seen.

    Using .read() will dump the actual data

    Empty File

    >>> image=open("meow.jpg",'rb').read()
    >>> image
    ''
    

    Image File

    >>> image=open("how.png",'rb').read()
    >>> image
    '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00...
    

    Both of these will not work with the call api.put_photo as you have seen and Klaus D. mentioned the call should be without read()

    So this call

    api.put_photo(image=open("path_to/my_image.jpg",'rb').read(), message='Here's my image')

    actually becomes

    api.put_photo('\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00...', message='Here's my image')

    Which is just a string, which isn't what is wanted.

    One needs the image reference <open file 'how.png', mode 'rb' at 0x1085b2390>