简体   繁体   中英

Post image to facebook via python and graphapi

Hoping to get some help with error I am getting. I am able to authenticate and post text to a users wall (so I am thinking that I am properly authenticated).

The code I am using is as follows:

self.client = GraphAPI(ACCESS_TOKEN)
self.client.put_photo(open("test.jpg"), "test post from app - please ignore")

I get the following error:

Traceback (most recent call last):
  File "J:\Projects\python Code\pyhton test programs\upload posts to facebook v3.py", line 69, in OnFacebookPublishButton
    self.client.put_photo(open("test.jpg"), "test post from app - please ignore")
  File "C:\Python27\lib\site-packages\facebook.py", line 231, in put_photo
    raise GraphAPIError(response)
facebook.GraphAPIError: Your photos couldn't be uploaded. Photos should be saved as JPG, PNG, GIF, BMP or TIFF files.

I have verified using the windows explore, ACDSee, Photoshop, Paint and Faststone, that the jpg are valid.

Try this:

self.client = GraphAPI(ACCESS_TOKEN)
with open("test.jpg","rb") as image:
    self.client.put_photo(image, "test post from app - please ignore")

the open function takes several parameters one of which is the mode in which it will open, your error is most likely that you did not specify the open mode after the file name.

Another important thing is that when you open a file, you also need to close it, so your code falters in the fact that it doesn't seem to include a .close() function call.

In my suggestion I open the image as read (binary) mode and use the with statement to avoid the closing statement.

I suggest you take some time to learn about the open function by reading the official docs https://docs.python.org/2/library/functions.html#open

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