简体   繁体   English

通过python和graphapi将图像发布到Facebook

[英]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. 我已经使用Windows探索,ACDSee,Photoshop,Paint和Faststone验证了jpg是有效的。

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. 另一个重要的事情是,当您打开文件时,您还需要关闭它,因此您的代码看起来似乎并不包含.close()函数调用,因而使您的代码步履蹒跚。

In my suggestion I open the image as read (binary) mode and use the with statement to avoid the closing statement. 在我的建议下,我将图像以读取(二进制)模式打开,并使用with语句避免使用close语句。

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 我建议您花一些时间通过阅读官方文档https://docs.python.org/2/library/functions.html#open了解开放功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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