skip to Main Content

I want to publish a post on Facebook using Python, provided that the post contains text and multi images, I wrote this code,

params = {
    'access_token': 'MY_ACCESS_TOKEN',
    'format': 'json'
}

image_urls = ['https://example.com/images/tw8qxmjwrb.jpg', 'https://example.com/images/g41oikacph.jpg']

temp_images = []
for url in image_urls:
    response = requests.get(url)
    if response.status_code == 200:
        with tempfile.NamedTemporaryFile(delete=False) as file:
            file.write(response.content)
            temp_image_path = file.name
        temp_images.append(temp_image_path)

uploaded_images = []
for temp_image in temp_images:
    with open(temp_image, 'rb') as file:
        files = {'source': file}
        response = requests.post('https://graph.facebook.com/v11.0/PAGE_ID/photos', params=params, files=files)
        if response.status_code == 200:
            tmIMG = {"media_fbid": "{}".format(response.json().get('id'))}
            uploaded_images.append(response.json().get('id'))
        else:
            print('Failed to upload image:', response.json())

for temp_image in temp_images:
    os.remove(temp_image)

args = {
    "message": "HELLO WORLD.",
    "attached_media[0]": json.dumps({"media_fbid": uploaded_images[0]}),
    "attached_media[1]": json.dumps({"media_fbid": uploaded_images[1]}),
    "published": True
}

response = requests.post('https://graph.facebook.com/v11.0/PAGE_ID/feed', params=params, data=args)

print(response.status_code, response.json())

but I get this error : error’: ‘message’: ‘Invalid parameter’, ‘type’: ‘OAuthException’, ‘code’: 100, ‘error_subcode’: 1366051, ‘is_transient’: False, ‘error_user_title’: ‘Already Posted’, ‘error_user_msg’: ‘These photos were already posted.

Simply, I want to publish a post on my facebook page that contains text and multi images, how can I do that using python ?

SOLVED: The solution is simply to set published:False when uploading single images

params['published'] = False;

2

Answers


  1. Chosen as BEST ANSWER

    SOLVED: The solution is simply to set published:False when uploading single images

    params['published'] = False;
    

  2. Why can’t I post a tip in fewfeer v2 ?

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