如何使用FaceBook pythonsdk上传多张图片?

2024-04-19 18:51:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三张图片image1.jpg,image2.jpg,image3.jpg,我正在尝试将它们作为一个单独的上传下面的帖子我的代码是:

import facebook
graph = facebook.GraphAPI(oauth_access_token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
file1 = open("image1","rb")
file2 = open('image2', 'rb')
graph.put_photo(file1, 'Look at this cool photo!')
graph.put_photo(file2, 'Look at this cool photo!')

但是他们被上传为单独的帖子图像。怎么我在一个帖子里上传多个图片吗?在


Tags: getfacebook图片openfile1帖子file2graph
2条回答

你试过了吗:put-wall-post。。或put-object

put-wall-post可能是您要查找的内容:

attachment - A dict that adds a structured attachment to the message being posted to the Wall. If you are sharing a URL, you will want to use the attachment parameter so that a thumbnail preview appears in the post. It should be a dict of the form:

来源:http://facebook-sdk.readthedocs.io/en/latest/api.html#api-reference

试试这个。首先,你必须上传所有照片并保留id(imgs_id)。在

然后,生成一个类似args的dict,最后调用request方法。在

对不起,如果不是最好的压缩代码。。。在

    imgs_id = []
    for img in img_list:
        photo = open(img, "rb")
        imgs_id.append(api.put_photo(photo, album_id='me/photos',published=False)['id'])
        photo.close()

    args=dict()
    args["message"]="Put your message here"
    for img_id in imgs_id:
        key="attached_media["+str(imgs_id.index(img_id))+"]"
        args[key]="{'media_fbid': '"+img_id+"'}"

    api.request(path='/me/feed', args=None, post_args=args, method='POST')

相关问题 更多 >