gpt4 book ai didi

python - 如何使用 FaceBook Python SDK 上传多张图片?

转载 作者:太空狗 更新时间:2023-10-30 01:38:03 25 4
gpt4 key购买 nike

我有三张图片 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!')

但它们会作为单独的帖子以单独的图片上传。如何在单个帖子中上传多张图片?

最佳答案

如果有人希望在 2021 年使用 Python 中的图形 API 发布多张图片或一段视频

import requests

auth_token = "XXXXXXXX"

def postImage(group_id, img):
url = f"https://graph.facebook.com/{group_id}/photos?access_token=" + auth_token

files = {
'file': open(img, 'rb'),
}
data = {
"published" : False
}
r = requests.post(url, files=files, data=data).json()
return r

def multiPostImage(group_id):
imgs_id = []
img_list = [img_path_1, img_path_2]
for img in img_list:
post_id = postImage(group_id ,img)

imgs_id.append(post_id['id'])

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+"'}"
url = f"https://graph.facebook.com/{group_id}/feed?access_token=" + auth_token
requests.post(url, data=args)

multiPostImage("426563960691001")

页面的工作方式相同,使用 page_id 而不是 group_id

发布视频

def postVideo(group_id, video_path):
url = f"https://graph-video.facebook.com/{group_id}/videos?access_token=" + auth_token
files = {
'file': open(video_path, 'rb'),
}
requests.post(url, files=files)

关于python - 如何使用 FaceBook Python SDK 上传多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27523313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com