skip to Main Content

I’m using facebook graph API to update/edit the text of a specific post on my page

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')

now I’m trying to add a local image (stored in the same folder of the python script) to this post but I don’t understand how to properly do it, I tried

fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', source = open('out.png', 'rb'))

and

fb.put_object(parent_object=page_id+'_'+post_id, connection_name='', message='new text', object_attachment = open('out.png', 'rb'))

but none of them works. Hints?

p.s. these are the permission of my app

pages_show_list
pages_read_engagement
pages_read_user_content
pages_manage_posts
pages_manage_engagement

EDIT: I tried with the function put_photo but it creates a new post adding the image to it, while I need to add an image to an already existing post

2

Answers


  1. I made a complete selenium script in python which can handle the same task :

    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")
    

    All you need to do is to change the variables given above
    Note:- It is working dated 9 Dec, 2020

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search