M
M
Maxim2016-09-15 14:14:08
Python
Maxim, 2016-09-15 14:14:08

How to post multiple photos on facebook wall?

Welcome all!
For the third day I’m trying to post several photos on the facebook wall, they only describe the post of one photo on the network, I’ve already tried all the methods, but I don’t fast more than one, I read that you can post an album with photos, went through the documentation, it turns out the method is outdated and the campaign is not supported, but maybe I'm wrong, at least I tried it and it didn't work.
I found a solution in php , I don’t understand its syntax, I tried to do it myself, but even in any way
Who understood fb or knows how to implement it? I will be very grateful for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-09-23
@maximkv25

So the solution to my question arrived, as always, everything turns out to be much simpler than you imagine)

# получаю список фото строкой, является прямой ссылкой типа http://image.jpg
# можно так же открывать файлы привычным способом f = open('file_path', 'rb')
import requests

files = 'http://img.jpg, http://img1.jpg, http://img2.jpg, '
token = access_token # (токен пользователя)

# передаем функции photo_id список файлов и наш токен
media = photo_id(file, token)
                # post on fb
                url = 'https://graph.facebook.com/me/feed?'
                data = {
                    'access_token': user.fb_token,
                    # так же можем добавлять дополнительно links, video, files и т.д.
                }
                i = 0
                for id in media:
                    # проходимся по нашему списку и формируем словарь
                    data.update({'attached_media[%d]' % (i): '{"media_fbid": "%s"}' % (id)})
                    i += 1
                resp = requests.post(url, data=data)


# в результате получаем список id с успешно загруженными фотками 
def photo_id(files, token):
    images = files.split(',')
    media = []
    for img in images:
            url = 'https://graph.facebook.com/me/photos?published=false'
            data = dict(access_token=token, url=img)
            resp = requests.post(url, data)
            res = json.loads(resp.text)['id']
            media.append(res)
    return media

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question