简体   繁体   English

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

[英]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. 我正在尝试使用Python和GraphApi将图像发布到用户Facebook墙上。 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 = GraphAPI(ACCESS_TOKEN)

self.client.put_object("me", "feed", message="MyText posted on FB wall") self.client.put_object(“ me”,“ feed”,message =“在FB墙上张贴的MyText”)

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. 所以我的问题是,必须使用/添加什么参数或配置才能将图像从用户计算机(特别是Windows,但从任何OS)发布到墙上。 I have found many references to url references to images but I want to directly post from desktop to wall. 我发现了很多对图像的url引用的引用,但是我想直接从桌面发布到墙上。

Through my work with the python implementation of the graphapi I came across the answer to this question that I asked. 通过我对graphapi的python实现的研究,我遇到了我提出的这个问题的答案。 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). 从某种程度上可以看出,不可能将图像发布到Facebook墙上(使用URL链接除外)。 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. 您要做的是将图像添加到相册,添加图像说明(如果您选择,但不是必需的),该图像将被发布到相册,但它也会出现在Facebook墙上,因此您已将上述图片张贴到墙上。

The code for posting an image to an album is (I received help for this from another of my SO questions): 将图像发布到相册的代码是(我的另一个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)

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 仅供参考,还有一种方法可以在Facebook墙上显示图像,但是我无法使其完全发挥作用,但是如果有人想弄清楚我做错了什么,我会提及它

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: 理论上,如果您知道已经发布的图像的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)

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. 这对我有用,除了看到图像外,我只看到一个空白区域,但是该空白区域是可单击的,单击时会打开正确的facebook图像。 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. 所以我需要弄清楚为什么图像没有出现在墙上,但是现在我不需要了,所以也许有一天我会回到这一点。

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

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