我正在尝试编写一个为我上传视频的机器人,我已经让它工作并用它上传了一些视频。突然间,它似乎已停止工作,现在只返回以下错误:
啪。SSLWantWriteError: 操作未完成 (写入) (_ssl.c:2408)
我不明白这是从哪里来的,为什么,互联网没有帮助,我试图阅读它来自的文件,但这样做并不聪明。
这是我尝试上传视频的代码:
def upload(beatName, mainArtist, keywords, oneLiner):
CLIENT_SECRET_FILE = 'client_secret_file.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)
upload_date_time = DT.datetime(2020, 8, 27, 12, 30, 0).isoformat() + '.000Z'
title = "xxx"
description = 'xxx'
request_body = {
'snippet': {
'categoryI': 10,
'title': title,
'description': description,
'tags': keywords
},
'status': {
'privacyStatus': 'private',
'publishAt': upload_date_time,
'selfDeclaredMadeForKids': False,
},
'notifySubscribers': True
}
mediaFile = MediaFileUpload('E:Everything.mp4')
response_upload = service.videos().insert(
part = 'snippet,status',
body = request_body,
media_body = mediaFile
).execute()
错误的原因可能是什么,我该如何尝试找到解决方案?
编辑:
按照评论者的建议更新我的客户端后(这确实是一个版本太旧了),我现在遇到了一个不同的错误,我也无法处理:
oauthlib.oauth2.rfc6749.errors.UnsupportedGrantTypeError: (unsupported_grant_type) Invalid grant_type:
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "C:/Users/ricsi/Desktop/Automatic Beat Uploader/main.py", line 34, in <module>
AutomaticUploader().run()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivyapp.py", line 855, in run
runTouchApp()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivybase.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivycorewindowwindow_sdl2.py", line 747, in mainloop
self._mainloop()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivycorewindowwindow_sdl2.py", line 479, in _mainloop
EventLoop.idle()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivybase.py", line 342, in idle
self.dispatch_input()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivybase.py", line 327, in dispatch_input
post_dispatch_input(*pop(0))
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivybase.py", line 233, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivycorewindow__init__.py", line 1402, in on_motion
self.dispatch('on_touch_down', me)
File "kivy_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivycorewindow__init__.py", line 1418, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivyuixwidget.py", line 549, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivyuixwidget.py", line 549, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivyuixbehaviorsbutton.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packageskivylangbuilder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:UsersricsiDesktopAutomatic Beat Uploaderautomaticuploader.kv", line 53, in <module>
on_press: root.upload()
File "C:/Users/ricsi/Desktop/Automatic Beat Uploader/main.py", line 27, in upload
upload(beatName, mainArtist, keywords, oneLiner)
File "C:UsersricsiDesktopAutomatic Beat Uploaderyoutube.py", line 144, in upload
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
File "C:UsersricsiDesktopAutomatic Beat UploaderGoogle.py", line 31, in Create_Service
cred = flow.run_local_server()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesgoogle_auth_oauthlibflow.py", line 460, in run_local_server
self.fetch_token(authorization_response=authorization_response)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesgoogle_auth_oauthlibflow.py", line 281, in fetch_token
self.client_config['token_uri'], **kwargs)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesrequests_oauthliboauth2_session.py", line 360, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749clientsbase.py", line 421, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749parameters.py", line 431, in parse_token_response
validate_token_parameters(params)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749parameters.py", line 438, in validate_token_parameters
raise_from_error(params.get('error'), params)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749errors.py", line 405, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.UnsupportedGrantTypeError: (unsupported_grant_type) Invalid grant_type:
根据请求添加调试日志(敏感数据已更改):
INFO:google_auth_oauthlib.flow:"GET /?state=J4aIHazfWta0WBvU5X5mtHzb6Xh1hT&code=4/3gGqpfkFDfV9nVgE4PkISwAB0pgFkD9-CcukEOhLk_FMxvsj0Ca9CBSpEvBpU0nzN63gbjeP2_WkfMaTgRa3gnA&scope=https://www.googleapis.com/auth/youtube.upload HTTP/1.1" 200 65
DEBUG:requests_oauthlib.oauth2_session:Encoding `client_id` "36097-31saf6e4sdfDAG3amuivcki8vgo3rmfgj.apps.googleusercontent.com" with `client_secret` as Basic auth credentials.
DEBUG:requests_oauthlib.oauth2_session:Requesting url https://oauth2.googleapis.com/token using method POST.
DEBUG:requests_oauthlib.oauth2_session:Supplying headers {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'} and data {'grant_type': 'authorization_code', 'code': '4/3gGqpfkFDfV9nVgE4PkISwAB0pgFkD9-CcukEOhLk_FMxvsj0Ca9CBSpEvBpU0nzN63gbjeP2_WkfMaTgRa3gnA', 'redirect_uri': 'http://localhost:8080/'}
DEBUG:requests_oauthlib.oauth2_session:Passing through key word arguments {'timeout': None, 'auth': <requests.auth.HTTPBasicAuth object at 0x000001CD14ECFAC8>, 'verify': True, 'proxies': None}.
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): oauth2.googleapis.com:443
DEBUG:urllib3.connectionpool:https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 400 None
DEBUG:requests_oauthlib.oauth2_session:Request to fetch token completed with status 400.
DEBUG:requests_oauthlib.oauth2_session:Request url was https://oauth2.googleapis.com/token
DEBUG:requests_oauthlib.oauth2_session:Request headers were {'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Length': '172', 'Authorization': 'Basic NjY0NdfwDE0ODEyLXRtbnE0dGddfgEx4azZndTNq3dDU2c3Nodm45tLmGRFwcHMuZ29456FGertxldXNlcmNvbnRlbnQuY29df3Q3E1VXpRDRpNDFqUTlfeTY5bw=='}
DEBUG:requests_oauthlib.oauth2_session:Request body was grant_type=authorization_code&code=4%2F3gEwdk40hc5ItuvD-JBsg4fdjKsx534dhf7df5KtdGhxIGS5646gh56K61-tW5cxOz34tG6Sg8Adysic3u12I346StHbT9Y4564Shg&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F
DEBUG:requests_oauthlib.oauth2_session:Response headers were {'Content-Type': 'application/json; charset=utf-8', 'Vary': 'Origin, X-Origin, Referer', 'Content-Encoding': 'gzip', 'Date': 'Thu, 27 Aug 2020 22:08:18 GMT', 'Server': 'scaffolding on HTTPServer2', 'Cache-Control': 'private', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'X-Content-Type-Options': 'nosniff', 'Alt-Svc': 'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked'} and content {
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: "
}.
DEBUG:requests_oauthlib.oauth2_session:Invoking 0 token response hooks.
Traceback (most recent call last):
File "<input>", line 12, in <module>
File "C:UsersricsiDesktopAutomatic Beat UploaderGoogle.py", line 31, in Create_Service
cred = flow.run_local_server()
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesgoogle_auth_oauthlibflow.py", line 460, in run_local_server
self.fetch_token(authorization_response=authorization_response)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesgoogle_auth_oauthlibflow.py", line 281, in fetch_token
self.client_config['token_uri'], **kwargs)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesrequests_oauthliboauth2_session.py", line 360, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749clientsbase.py", line 421, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749parameters.py", line 431, in parse_token_response
validate_token_parameters(params)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749parameters.py", line 438, in validate_token_parameters
raise_from_error(params.get('error'), params)
File "C:UsersricsiAnaconda3envsAutomatic Beat Uploaderlibsite-packagesoauthliboauth2rfc6749errors.py", line 405, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.UnsupportedGrantTypeError: (unsupported_grant_type) Invalid grant_type:
(免责声明:这不是答案,只是试图解决OP的问题。
请在源文件中定义以下派生类:
class InstalledAppFlow2(InstalledAppFlow):
def fetch_token(self, **kwargs):
kwargs.setdefault("client_secret", self.client_config["client_secret"])
kwargs.setdefault("code_verifier", self.code_verifier)
return self.oauth2session.fetch_token(
self.client_config["token_uri"],
include_client_id = True,
**kwargs)
然后替换
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
跟
flow = InstalledAppFlow2.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
InstalledAppFlow2
的目的是使flow
对象传递给函数OAuth2Session.fetch_token
参数include_client_id = True
;此参数将指示OAuth2Session.fetch_token
在对服务器的授权调用的请求正文中传递client_id
和client_secret
。
然后再次验证为其行Request body
生成的日志文件是否具有client_id=...
和client_secret=...
。此外,Request header
线不应该再有Authorization: Basic ...
.