簡體   English   中英

通過python和Graphapi將圖像上傳到Facebook牆

[英]Upload images to Facebook wall via python and Graphapi

我正在嘗試使用Python和GraphApi將圖像發布到用戶Facebook牆上。 我可能已經找到答案,那就是我想做的事是不可能的,但我希望不是,所以無論如何我都會尋求幫助。

我可以使用以下方法成功驗證身份並將文本發布到牆上:

self.client = GraphAPI(ACCESS_TOKEN)

self.client.put_object(“ me”,“ feed”,message =“在FB牆上張貼的MyText”)

所以我的問題是,必須使用/添加什么參數或配置才能將圖像從用戶計算機(特別是Windows,但從任何OS)發布到牆上。 我發現了很多對圖像的url引用的引用,但是我想直接從桌面發布到牆上。

通過我對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牆上顯示圖像,但是我無法使其完全發揮作用,但是如果有人想弄清楚我做錯了什么,我會提及它

理論上,如果您知道已經發布的圖像的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)

這對我有用,除了看到圖像外,我只看到一個空白區域,但是該空白區域是可單擊的,單擊時會打開正確的facebook圖像。 所以我需要弄清楚為什么圖像沒有出現在牆上,但是現在我不需要了,所以也許有一天我會回到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM