我尝试使用 Python 将照片或视频上传到 Facebook 页面,但我得到了 HTTPError:HTTP 错误 400:错误请求。但当我用 form 代替时就可以了。 让我向您展示代码。
这是表单代码。
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="https://graph-video.facebook.com/videos/PAGE_ID/photos?access_token=ACCESS_TOKEN"
method="POST">
<input name="file" type="file">
<input type="submit" value="Upload" />
</form>
</body>
</html>
这是我的 Python 代码。
video = open(args[0])
url = 'https://graph-video.facebook.com/videos/PAGE_ID'
data = {'access_token': 'ACCESS_TOKEN',
'title': 'test',
'description': 'test',
'source' : video
}
data1 = urllib.urlencode(data)
req = urllib2.Request(url, data1)
r = urllib2.urlopen(req)
我认为 access_token 不是问题,因为它在我使用表单时有效。
请告诉我如何使用Python上传视频或照片。谢谢。
最佳答案
这对我有用。
import requests
url='https://graph-video.facebook.com/100000198728296/videos?access_token='+str(access)
path="/home/abc.mp4"
files={'file':open(path,'rb')}
flag=requests.post(url, files=files).text
print flag
成功上传视频后,flag 将返回包含视频 ID 的 json
关于python - 使用 Python 将照片或视频上传到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691636/