python - 在 Cron 错误上运行 YouTube API 上传脚本
问题描述
在 Cron 上运行 YouTube API 上传脚本总是会出现此错误(取自 cron.log):
/home/ubuntu/scripts/resources/client_secret.json-youtube-v3-(['https://www.googleapis.com/auth/youtube.upload'],)
['https://www.googleapis.com/auth/youtube.upload']
Traceback (most recent call last):
File "ytuploader.py", line 18, in <module>
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
File "/home/ubuntu/scripts/server_scripts/zivcovek/Google.py", line 31, in Create_Service
cred = flow.run_local_server()
File "/home/ubuntu/.local/lib/python3.8/site-packages/google_auth_oauthlib/flow.py", line 459, in run_local_server
local_server = wsgiref.simple_server.make_server(
File "/usr/lib/python3.8/wsgiref/simple_server.py", line 154, in make_server
server = server_class((host, port), handler_class)
File "/usr/lib/python3.8/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/lib/python3.8/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/usr/lib/python3.8/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.8/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
'ps -fA | grep python'输出:
root 432 1 0 17:54 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root 502 1 0 17:54 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
ubuntu 1352 1351 0 18:28 ? 00:00:00 /bin/sh -c /usr/bin/python3 /home/ubuntu/scripts/server_scripts/zivcovek/yt2yt.py >> /home/ubuntu/scripts/server_scripts/zivcovek/logs/cron.log 2>&1
ubuntu 1353 1352 0 18:28 ? 00:00:00 /usr/bin/python3 /home/ubuntu/scripts/server_scripts/zivcovek/yt2yt.py
ubuntu 1634 1538 0 18:55 pts/0 00:00:00 grep --color=auto python
如果我用: 杀死 Python 服务kill -9 $(ps -A | grep python | awk '{print $1}')
,服务就会停止,但是一旦 Cron 运行脚本,我就会得到同样的错误。
我用于上传的代码如下:
from Google import Create_Service
from googleapiclient.http import MediaFileUpload
CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'youtube'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
file = 'test.mp4'
title = 'test'
video_description = "test"
request_body = {
'snippet':{
'categoryId': 19,
'title': video_title,
'description' : video_description,
},
'status': {
'privacyStatus': 'unlisted',
}
}
mediaFile = MediaFileUpload(file, chunksize=-1, resumable=True)
response_upload = service.videos().insert(
part = 'snippet, status',
body = request_body,
media_body = mediaFile
).execute()
print('Video Uploaded')
如我所见,上传代码中的“service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)”行与之前已经创建的服务 Cron 之间存在冲突。
有想法该怎么解决这个吗?也许在另一个端口上运行 Create_Service,但我该怎么做呢?
谢谢!
解决方案
推荐阅读
- laravel - 如何解决 PHP 致命错误:内存不足,当我为身份验证文件尝试此命令“composer require laravel/ui”时。?
- python - 百分比直方图未正确显示
- properties - SysML/UML 在决策中使用实例的属性
- identityserver4 - 从身份服务器4获得未经授权的响应
- stargazer - 由于 dcolumn,R stargazer 函数生成的表未对齐
- python - 如何更改 matplotlib x 刻度的方向
- api - 我应该使用 PUT over POST 来添加“关系”吗?
- c# - 导出到文本文件时 SelectCommand 初始化错误
- react-native - 在 react native 中安装 formik 时出现错误
- git - 将代码添加到私有分支的 Git 命令