如何使用 API 和 python 将图像添加到现有的 facebook post?
How to add an image to an existing facebook post using API and python?
我正在使用 facebook graph API 来 update/edit 我页面上特定 post 的文本
import facebook
page_token = '...'
fb = facebook.GraphAPI(access_token = page_token, version="2.12")
page_id = '...'
post_id = '...'
fb.put_object(parent_object = page_id + '_' + post_id,
connection_name = '',
message = 'new text')
现在我正在尝试向此 post 添加本地图像(存储在 python 脚本的同一文件夹中),但我不知道如何正确地执行此操作,我尝试过
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', source = open('out.png', 'rb'))
和
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', object_attachment = open('out.png', 'rb'))
但其中 none 有效。提示?
p.s。这些是我的应用程序的许可
pages_show_list
pages_read_engagement
pages_read_user_content
pages_manage_posts
pages_manage_engagement
编辑:我尝试使用函数 put_photo
但它创建了一个新的 post 添加图像,而我需要将图像添加到已经存在的 post
https://developers.facebook.com/docs/graph-api/reference/page/photos/
https://developers.facebook.com/docs/graph-api/reference/photo/
文档说明您无法执行更新。
我在 python 中制作了一个完整的 selenium 脚本,它可以处理相同的任务:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
import pyautogui as pag
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", {
"profile.default_content_setting_values.notifications": 1
})
#########CHANGE################
EMAIL_ADDRESS="" # Email Address of your facebook account
PASSWORD="" # Password of your facebook account
POST_URL="" # URL of your post ex:- https://www.facebook.com/USERNAME/posts/ID
IMG_PATH="C:\...\test.jpg" # complete path of image
re_edit=True # set True when you already edited the post otherwise set it to False if you are editing the post for the first time
##########CHANGE###############
re_edit_val="/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div[1]/div/div[1]/div/div[2]"
re_edit_val2="/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[1]/div[2]/div/div[1]/span/div/div/div/div/div[1]"
driver = webdriver.Chrome(options=option, executable_path='chromedriver.exe')
driver.set_window_position(-10000,0) # hiding the windows
print("Accessing facebook.com")
driver.get("https://www.facebook.com/")
print("checking credentials")
driver.find_element_by_xpath("//input[@id='email']").send_keys(EMAIL_ADDRESS)
driver.find_element_by_xpath("//input[@id='pass']").send_keys(PASSWORD)
driver.find_element_by_xpath("//input[@id='pass']").send_keys(Keys.RETURN)
print("credentials checked successfully")
time.sleep(1)
print("Accessing the URL of the post")
driver.get(POST_URL)
time.sleep(0.5)
print("getting the edit access from facebook.com")
driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[1]/div[1]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[2]/div/div[2]/div/div[3]/div").click() # clicking three dots
time.sleep(3)
if re_edit:
re_edit_val="/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div[1]/div/div[1]/div/div[3]"
re_edit_val2="/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[1]/div[2]/div/div[1]/div/span/div/div/div/div/div[1]"
driver.find_element_by_xpath(re_edit_val).click() # clicking the edit post
time.sleep(3)
print("Uploading the given image...")
driver.find_element_by_xpath(re_edit_val2).click() # clicking Image upload
time.sleep(0.5)
pag.write(IMG_PATH) # uploading file path
pag.press('enter')
time.sleep(1.5)
print("Finishing up...")
driver.find_elements_by_xpath("/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[2]/div")[0].click() # clicking save button
print("Photo uploaded")
你需要做的就是改变上面给出的变量
注意:- 工作日期为 2020 年 12 月 9 日
我正在使用 facebook graph API 来 update/edit 我页面上特定 post 的文本
import facebook
page_token = '...'
fb = facebook.GraphAPI(access_token = page_token, version="2.12")
page_id = '...'
post_id = '...'
fb.put_object(parent_object = page_id + '_' + post_id,
connection_name = '',
message = 'new text')
现在我正在尝试向此 post 添加本地图像(存储在 python 脚本的同一文件夹中),但我不知道如何正确地执行此操作,我尝试过
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', source = open('out.png', 'rb'))
和
fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', object_attachment = open('out.png', 'rb'))
但其中 none 有效。提示?
p.s。这些是我的应用程序的许可
pages_show_list
pages_read_engagement
pages_read_user_content
pages_manage_posts
pages_manage_engagement
编辑:我尝试使用函数 put_photo
但它创建了一个新的 post 添加图像,而我需要将图像添加到已经存在的 post
https://developers.facebook.com/docs/graph-api/reference/page/photos/ https://developers.facebook.com/docs/graph-api/reference/photo/
文档说明您无法执行更新。
我在 python 中制作了一个完整的 selenium 脚本,它可以处理相同的任务:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
import pyautogui as pag
option = Options()
option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
option.add_experimental_option("prefs", {
"profile.default_content_setting_values.notifications": 1
})
#########CHANGE################
EMAIL_ADDRESS="" # Email Address of your facebook account
PASSWORD="" # Password of your facebook account
POST_URL="" # URL of your post ex:- https://www.facebook.com/USERNAME/posts/ID
IMG_PATH="C:\...\test.jpg" # complete path of image
re_edit=True # set True when you already edited the post otherwise set it to False if you are editing the post for the first time
##########CHANGE###############
re_edit_val="/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div[1]/div/div[1]/div/div[2]"
re_edit_val2="/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[1]/div[2]/div/div[1]/span/div/div/div/div/div[1]"
driver = webdriver.Chrome(options=option, executable_path='chromedriver.exe')
driver.set_window_position(-10000,0) # hiding the windows
print("Accessing facebook.com")
driver.get("https://www.facebook.com/")
print("checking credentials")
driver.find_element_by_xpath("//input[@id='email']").send_keys(EMAIL_ADDRESS)
driver.find_element_by_xpath("//input[@id='pass']").send_keys(PASSWORD)
driver.find_element_by_xpath("//input[@id='pass']").send_keys(Keys.RETURN)
print("credentials checked successfully")
time.sleep(1)
print("Accessing the URL of the post")
driver.get(POST_URL)
time.sleep(0.5)
print("getting the edit access from facebook.com")
driver.find_element_by_xpath("/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[1]/div[1]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[2]/div/div[2]/div/div[3]/div").click() # clicking three dots
time.sleep(3)
if re_edit:
re_edit_val="/html/body/div[1]/div/div[1]/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div[1]/div/div[1]/div/div[3]"
re_edit_val2="/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[1]/div[2]/div/div[1]/div/span/div/div/div/div/div[1]"
driver.find_element_by_xpath(re_edit_val).click() # clicking the edit post
time.sleep(3)
print("Uploading the given image...")
driver.find_element_by_xpath(re_edit_val2).click() # clicking Image upload
time.sleep(0.5)
pag.write(IMG_PATH) # uploading file path
pag.press('enter')
time.sleep(1.5)
print("Finishing up...")
driver.find_elements_by_xpath("/html/body/div[1]/div/div[1]/div[1]/div[4]/div/div/div[1]/div/div[2]/div/div/div/form/div/div[1]/div/div/div[3]/div[2]/div")[0].click() # clicking save button
print("Photo uploaded")
你需要做的就是改变上面给出的变量
注意:- 工作日期为 2020 年 12 月 9 日