python - 视频从本地驱动器上传到 Facebook

标签 python facebook-graph-api python-requests

我正在尝试从本地驱动器上传视频,但有些内容没有通过 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/

相关文章:

python - 我可以让 ipython 从调用代码中退出吗?

python - 将命令行参数传递给Python中导入的另一个文件

facebook - Facebook YouTube嵌入og:meta标签突然退出工作

python - Telegram Bot "chat not found"

python - 如何在python中抓取网页上id = "firstheading"之后的所有信息?

python - 如何使用 DRF 的 ModelSerializer 创建一个 django 用户

facebook-graph-api - 在 Facebook 上分享视频失败

facebook-graph-api - 新的 Facebook 权限 API (v2.0) 支持

python-3.x - 忽略 SSL 证书验证有什么影响?

python - 使用请求 python 在谷歌上进行简单查询时出现错误 429