通过python和Graphapi将图像上传到Facebook墙

时间:2014-04-26 21:17:38

标签: python image facebook-graph-api

我正在尝试使用Python和GraphApi将图像发布到用户的Facebook墙上。我可能已经找到了答案,我想做的事情是不可能的,但我希望不是这样,无论如何我都会寻求帮助。

我可以使用以下方式成功验证并将文字发布到墙上:

self.client = GraphAPI(ACCESS_TOKEN)

self.client.put_object(" me"," feed",message =" MyText张贴在FB墙上")

所以我的问题是必须使用/添加哪些参数或配置才能将图像从用户计算机(特别是Windows,但是从任何操作系统)发布到墙上。我发现很多引用url对图像的引用,但我想直接从桌面发布到墙上。

1 个答案:

答案 0 :(得分:0)

通过我对graphapi的python实现的工作,我遇到了我问的这个问题的答案。在某种程度上,可以看到不可能将图像发布到Facebook墙(除了使用URL链接)。以另一种方式看待它是非常可能的。你要做的是将图像添加到相册,添加图像的描述(如果你选择,但不是必需的),图像将被发布到相册但它也将出现在Facebook墙上,因此你把所说的图像发布到了墙上。

将图像发布到相册的代码是(我从另一个SO问题中获得了帮助):

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)

仅供参考,还有另一个选项可以将图像显示到脸谱墙上,但是我无法让它完全发挥作用,但我会提到它,以防有人想知道我做错了什么

如果您知道已发布图像的facebook imageid,理论上(根据facebook Graph Api文档,您可以使用object_attachment在墙上的帖子中引用所述图像。所以代码看起来像这样:

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)

这对我有用,除了看到图像,我只看到一个空白区域,但该空白区域是可点击的,当点击它打开正确的脸书图像。所以我需要搞清楚为什么图像没有出现在墙上,但是现在我不需要它,所以也许有一天我会回到这里。

相关问题