简体   繁体   中英

Upload images to Facebook wall via python and Graphapi

I am trying to post images to a users Facebook wall with Python and the GraphApi. I may have found that the answer is that what I want to do is not possible, but I hope not so I am going to ask for help anyway.

I can successfully authenticate and post text to a wall using:

self.client = GraphAPI(ACCESS_TOKEN)

self.client.put_object("me", "feed", message="MyText posted on FB wall")

So my question is what parameters or configuration must be used/added in order to post an image from the user computer (specifically Windows, but from any OS) to the wall. I have found many references to url references to images but I want to directly post from desktop to wall.

Through my work with the python implementation of the graphapi I came across the answer to this question that I asked. In a way it can be looked at that it is impossible to post an image to a facebook wall (with the exception of using a url link). Looking at it another way it is VERY possible. What you do is add the image to an album, add a description of the image (if you choose to, but it is not required), and the image will be posted to the album BUT it will also appear on the facebook wall, thus you have posted said image to the wall.

The code for posting an image to an album is (I received help for this from another of my SO questions):

caption = "This will be the description of the image in the album and also the message that appears on the wall"  # caption = '' is acceptable if you do not wish to have a message
albumid = ''  # this can be the album id of a facebook album or it can be left blank
with open("test.jpg","rb") as image:
        posted_image_id = GraphAPI.put_photo(image, caption, albumid)

FYI, there is another option for displaying an image to a facebook wall, but I was not able to get it to fully work, but I will mention it in case someone wants to figure out what I was doing wrong

If you know the facebook imageid of an already posted image, in theory (according to the facebook Graph Api documentation, you can reference said image on a wall post using object_attachment. So the code would look something like this:

msg = "This is the message that I want displayed on my wall"
imageid = facebook_image_id  #to get this you will have execute a read via the graph api and select the image you want to "reference" on your wall post
GraphAPI.put_object("me", "feed", message=msg,object_attachment=imageid)

This worked for me except instead on seeing the image, I just see a blank area, but that blank area IS click-able and when clicked it opens up the correct facebook image. So I need figure out why the image does not appear on the wall, but for now I do not need it, so maybe one day I will come back to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM