Python自动上传视频到YouTube

这边有个需求就是实现,自动上传视频到youtube上,我这次试用的语言是Python,网上找了一些资料,选择了github上的一个项目:https://github.com/tokland/youtube-upload

先贴下谷歌开发者文档:https://developers.google.com/youtube/registering_an_application

https://developers.google.com/apis-explorer

阅读 youtube-upload 上的说明,按照以下步骤进行实现:

安装 依赖包:

1
pip3 install --upgrade google-api-python-client oauth2client progressbar2

获取仓库代码并安装:

1
2
3
4
5
6

$ wget https://github.com/tokland/youtube-upload/archive/master.zip
$ unzip master.zip
$ cd youtube-upload-master
$ sudo python3 setup.py install

Google 控制台上进行设置,添加凭证:

1、转到控制台:https://console.developers.google.com/

2、创建一个项目

3、API 和服务,>启用API和服务,找到 YouTube Data API v3 并开启

4、侧边栏>凭证:创建凭证->OAuth客户端ID->选择桌面客户端(项目介绍里选择Other,但我没有找到,刚开始选择了web客户端,之后报错如下:),所以经验证选择桌面客户端,然后输入名称,保存即可

1
The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type. You can create a Client ID for native application at https://console.developers.google.com/apis/credentials/oauthclient

5、下载Json文件:client_secrets.json,放至路径如下:

1
2
3
4
5
6
# 如果是Windows
C:\Users\admin\.client_secrets.json
# 如果是Linux
/root/.client_secrets.json

# 因为 youtube-upload 项目代码的目录是这,也可以自行修改,但为了防止发生什么错误,我还是选择了原项目存放的地址

运行

进入目录youtube-upload-master/bin,执行一个简单测试看是否成功运行:

1
python3 youtube-upload --title="A.S. Mutter" a.mp4

这时候会 提醒,在浏览器上打开列出的地址,如:Check this link in your browser: https://accounts.google.com/o/oauth2/a....

你只要在浏览器 上打开这个链接,然后进行相关的授权就可以,成功的话会得到一串码,类似于:

4/1AX4XfWjr1XXXXXX_82Zfco8FgO9tjXXX-D1k-cvItsJy-ZV3N-qcJirXXX

然后粘贴到命令行中,回车,得到:Start upload: a.mp4 Video URL: https://www.youtube.com/watch?v=Da2gAQURn2k

上传成功!

错误

如果有以下错误:

1
2
错误 403: access_denied
The developer hasn’t given you access to this app. It’s currently being tested and it hasn’t been verified by Google. If you think you should have access, contact the developer (XXX@gmail.com).

只需要跳转到OAuth 同意屏幕https://console.cloud.google.com/apis/credentials/consent

添加测试者即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
youtube-upload \
--title="A.S. Mutter" \
--description="A.S. Mutter plays Beethoven" \
--category="Music" \
--tags="mutter, beethoven" \
--recording-date="2011-03-10T15:32:17.0Z" \
--default-language="en" \
--default-audio-language="en" \
--client-secrets="my_client_secrets.json" \
--credentials-file="my_credentials.json" \
--playlist="My favorite music" \
--embeddable=True|False \
anne_sophie_mutter.flv
1
2
3
4
5
错误:ERROR: Could not find a version that satisfies the requirement googleapiclient (from versions: none) ERROR: No matching distribution found for googleapiclient

pip3 install google-api-python-client==1.7.9
pip3 install google-auth-httplib2==0.0.3
pip3 install google-auth-oauthlib==0.4.0
1
2
3
4
5
6
# 踩坑经历,因为刚开始选择的类型为 web客户端,所以报这个错误的话,则需要配置回调地址
错误 400: redirect_uri_mismatch
The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type. You can create a Client ID for native application at https://console.developers.google.com/apis/credentials/oa

# 添加回调地址:
http://localhost:8080/oauth2callback

以上记录。

作者

Fahsa

发布于

2021-11-03

更新于

2021-11-03

许可协议

评论