【问题标题】:Uploading photos or videos to Facebook using Python使用 Python 将照片或视频上传到 Facebook
【发布时间】:2013-07-15 12:35:51
【问题描述】:

我尝试使用 Python 将照片或视频上传到 Facebook 页面,但我得到了 HTTPError:HTTP 错误 400:错误请求。但是当我使用表单时没关系。 让我给你看代码。

这是表单的代码。

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

【问题讨论】:

  • 我的猜测是您的问题来自 enctype 您的第二个请求(Python)中缺少的部分。

标签: python facebook video upload


【解决方案1】:

这对我有用。

 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

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多