我已經在網上搜索了一個通過Python API(Python for Facebook)將照片上傳到Facebook的照片。像這樣的問題已經在stackoverflow之前問過,但沒有找到我已經找到工作的答案。使用Python API將圖像上傳到Facebook
我得到的工作是:
import facebook as fb
cfg = {
"page_id" : "my_page_id",
"access_token" : "my_access_token"
}
api = get_api(cfg)
msg = "Hello world!"
status = api.put_wall_post(msg)
,我已經定義了get_api(CFG)功能,因爲這
graph = fb.GraphAPI(cfg['access_token'], version='2.2')
# Get page token to post as the page. You can skip
# the following if you want to post as yourself.
resp = graph.get_object('me/accounts')
page_access_token = None
for page in resp['data']:
if page['id'] == cfg['page_id']:
page_access_token = page['access_token']
graph = fb.GraphAPI(page_access_token)
return graph
這確實張貼消息我頁面。 但是,如果我不想上傳圖片,那麼一切都會出錯。
# Upload a profile photo for a Page.
api.put_photo(image=open("path_to/my_image.jpg",'rb').read(), message='Here's my image')
我得到了可怕的GraphAPIError:(#324)需要上傳文件對計算器爲我工作的解決方案,其非。 如果我不是發出以下命令
api.put_photo(image=open("path_to/my_image.jpg",'rb').read(), album_path=cfg['page_id'] + "/picture")
我得到GraphAPIError:(#1)無法獲取圖片爲此,我一直沒能找到一條解決方案。
是否有人在那裏請指出我正確的方向爲我提供一個當前的工作示例?將不勝感激,謝謝!
的圖像必須是一個文件對象不是它的背景下,也是你的行中包含有故障的單引號。沒有測試過,你應該嘗試:'api.put_photo(image = open(「path_to/my_image.jpg」,'rb'),message ='這是我的圖像')''。 –
@KlausD。 是的,當我發佈這個問題的時候,我看到了我的錯誤引號,但是那時已經太晚了。但是,在我的真實代碼中,我沒有這樣的標記。 如果我嘗試你的建議 api.put_photo(image = open(「path_to/my_image.jpg」,'rb'),message ='Any message') 我仍然* GraphAPIError:(#324)需要上傳文件* 但謝謝你的建議! –
你能列出你正在使用的文件和目錄結構以及你的python調用開始於哪個目錄嗎 – phwd