想要通过Youtube API在vps上上传视频,但对Oauth不了解,幸亏GitHub上已经有前辈制作好了脚本,简简单单两个步骤,就可以用API上传视频啦!
GitHub地址:https://github.com/tokland/youtube-upload
第一步:安装youtube-upload,
$ pip install google-api-python-client $ pip install progressbar2 $ wget https://github.com/tokland/youtube-upload/archive/master.zip $ unzip master.zip $ cd youtube-upload-master $ sudo python setup.py install
第二步:注册API
- Go to the Google console.
- Create project.
- Side menu: APIs & auth -> APIs
- Top menu: Enabled API(s): Enable all Youtube APIs.
- Side menu: APIs & auth -> Credentials.
- Create a Client ID: Add credentials -> OAuth 2.0 Client ID -> Other -> Name: youtube-upload -> Create -> OK
- Download JSON: Under the section "OAuth 2.0 client IDs". Save the file to your local system.
- Use this JSON as your credentials file:
--client-secrets=CLIENT_SECRETS
前面几个步骤比较好完成,其实就是登录google,注册一个Youtube的OAuth 2.0认证的凭证,基本都是中文,挺好注册的。
倒数第二步,下载到本地的文件是:client_secret_xxxxx-xxxxx.apps.googleusercontent.com.json(每个人的文件名不同)。因为我是在VPS上运行,所以要用这个文件替换youtube-upload默认的json 。
默认的json地址为: /usr/local/share/youtube_upload/client_secrets.json
mv client_secret_xxxxx-xxxxxi.apps.googleusercontent.com.json /usr/local/share/youtube_upload/client_secrets.json
这样就不用每次上传时都输入命令选项了 --client-secrets=CLIENT_SECRETS
由于是OAuth认证,我们第一次上传视频时,我们要授权youtube-download:
youtube-upload --title="视频标题" XXXXXX.mp4
这个时候,命令行的最后一行应该是please input authorization code: (未截图,记不清原话)
倒数第二行,给出了一个google的授权链接。复制这个链接,并在浏览器中登录账号,点击授权,获得一个授权码。然后把这个验证码复制到命令行中,即可成功。
之后就再也不用输入授权码了。
youtube-upload 上传命令:
youtube-upload --title="this is a test video" /root/youtube/upload/test.mp4
其他选项:
$ 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" \ anne_sophie_mutter.flv更多选项请在作者主页查看: https://github.com/tokland/youtube-upload