我正在尝试从本地驱动器上传视频,但有些内容没有通过 python 的请求帖子上传文件
import requests
import json
accesstoken = '-----------------'
desc = 'This is test'
titl = 'Testing Video'
vidfbpath = '/tempvideos/0xjwseCVUlU.mp4'
source = open(vidfbpath, 'rb')
# need binary rep of this, not sure if this would do it
fburl = 'https://graph-video.facebook.com/v2.0/1098719680172720/videos?access_token='+str(accesstoken)
# put it all together to post to facebook
m = {'description': desc,
'title': titl,
'source': vidfbpath,}
r = requests.post(fburl, data=m).text
fb_res = json.loads(r)
输出返回 InsecurePlatformWarning: A true SSLContext object is not available。这会阻止 urllib3 正确配置 SSL,并可能导致某些 SSL 连接失败。欲了解更多信息,请参阅https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning 。 不安全平台警告
最佳答案
InsecurePlatformWarning
是警告,而不是错误。您仍有很大机会成功上传视频文件。
事实上,您的代码将发送内容类型为 application/x-www-form-urlencoded
的 POST HTTP 请求,并且会对表单数据进行适当的编码。这实际上并没有上传文件,它只是在 source
表单变量中发布文件的位置。
我认为您需要使用 multipart/form-data
内容类型上传文件,如 described这里。指定 mp4 文件的内容类型也是一个好主意。像这样的事情:
m = {'description': desc,
'title': titl,}
files = {'source': ('0xjwseCVUlU.mp4', open('/tempvideos/0xjwseCVUlU.mp4', 'rb'), 'video/mp4')}
r = requests.post(fburl, data=m, files=files)
关于python - 视频从本地驱动器上传到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35765817/