1

我尝试使用 Python 将照片或视频上传到 Facebook 页面,但出现 HTTPError: HTTP Error 400: Bad Request。但是当我使用表单时没关系。让我给你看代码。

这是表格的代码。

<!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 上传视频或照片。谢谢。

4

1 回答 1

4

这对我有用。

 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

成功上传视频后,标志将返回包含视频 ID 的json

于 2014-03-13T16:18:22.110 回答