因此,今天早些时候,我开始进行YouTube实验,该实验每10分钟更新一次缩略图。因此,我的Python代码基于youtube.thumbnails.set()上Google开发者页面上的代码示例:https://developers.google.com/youtube/v3/docs/thumbnails/set
我的代码如下:
def doCredentialStuff():
global youtube
global flow
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console(access_type='offline')
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
def updateYTthumb(filename):
try:
request = youtube.thumbnails().set(
videoId=VIDEO_ID_UPLOAD,media_body=MediaFileUpload(filename))
response = request.execute()
print(response)
except Exception as exc:
print("ERROR!!!! I got this exception: "+str(exc)+" at time "+str(time.time()))
出于我的目的,我想每10分钟上传一次缩略图。因此,在运行代码开始时,我只运行一次doCredentials()。然后,每隔10分钟,我会重新运行updateYTthumb(),在该处实际执行请求。 (此部分)
它非常适合2到12次迭代的任何地方! (20-120分钟)。但是,如果我让它放置足够长的时间,它最终总是会返回此错误:
[WinError 10053] An established connection was aborted by the software in your host machine
我不确定如何解决此问题:
doCredentialStuff()
中的某处获取刷新令牌,然后在updateYTthumb()
中使用它吗?错误是计算机上的某些东西中断了连接,您可以尝试以下操作:
尝试使用crontab进行及时的操作。
基本文档w.r.t.授权是OAuth 2.0 for Mobile & Desktop Apps。
此文档specifies,在OAuth流程上,您会收到短期访问令牌和长期刷新令牌。
Calling Google APIs部分指定,使用访问令牌可以发出需要授权的API调用(就像您的一样)。 Refreshing an access token部分指定访问令牌定期过期。
因此,每当当前访问令牌变为无效(即过期)时,您都必须使用API来交换一个新的访问令牌,使用您之前收到的刷新令牌(该令牌根本不会过期-除外)是用户的explicitly revoked。