kootenpv/yagmail

HTTP error 400: Bad request

jordimaggi opened this issue · 2 comments

Hi,
I am trying to use yagmail to send an email, when a long-running Jupyter cell finishes running on a Ubuntu 20.04 VM. It worked a few times last week. Now, however, all of a sudden, it throws this error:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-11-7cc45f210cea> in <module>
     25         print(message)
     26 
---> 27 yag.send(to, subject, body)

~/anaconda3/lib/python3.7/site-packages/yagmail/sender.py in send(self, to, subject, contents, attachments, cc, bcc, preview_only, headers, prettify_html, message_id, group_messages)
    151     ):
    152         """ Use this to send an email with gmail"""
--> 153         self.login()
    154         recipients, msg_string = self.prepare_send(
    155             to,

~/anaconda3/lib/python3.7/site-packages/yagmail/sender.py in login(self)
    202     def login(self):
    203         if self.oauth2_file is not None:
--> 204             self._login_oauth2(self.credentials)
    205         else:
    206             self._login(self.credentials)

~/anaconda3/lib/python3.7/site-packages/yagmail/sender.py in _login_oauth2(self, oauth2_info)
    242         except AttributeError:
    243             pass
--> 244         auth_string = self.get_oauth_string(self.user, oauth2_info)
    245         self.smtp.ehlo(oauth2_info["google_client_id"])
    246         if self.starttls is True:

~/anaconda3/lib/python3.7/site-packages/yagmail/sender.py in get_oauth_string(user, oauth2_info)
    232     @staticmethod
    233     def get_oauth_string(user, oauth2_info):
--> 234         return get_oauth_string(user, oauth2_info)
    235 
    236     def _login_oauth2(self, oauth2_info):

~/anaconda3/lib/python3.7/site-packages/yagmail/oauth2.py in get_oauth_string(user, oauth2_info)
     94 
     95 def get_oauth_string(user, oauth2_info):
---> 96     access_token, expires_in = refresh_authorization(**oauth2_info)
     97     auth_string = generate_oauth2_string(user, access_token, as_base64=True)
     98     return auth_string

~/anaconda3/lib/python3.7/site-packages/yagmail/oauth2.py in refresh_authorization(google_client_id, google_client_secret, google_refresh_token)
     89 
     90 def refresh_authorization(google_client_id, google_client_secret, google_refresh_token):
---> 91     response = call_refresh_token(google_client_id, google_client_secret, google_refresh_token)
     92     return response['access_token'], response['expires_in']
     93 

~/anaconda3/lib/python3.7/site-packages/yagmail/oauth2.py in call_refresh_token(client_id, client_secret, refresh_token)
     69     request_url = command_to_url('o/oauth2/token')
     70     encoded_params = urlencode(params).encode('UTF-8')
---> 71     response = urlopen(request_url, encoded_params).read().decode('UTF-8')
     72     return json.loads(response)
     73 

~/anaconda3/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

~/anaconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

~/anaconda3/lib/python3.7/urllib/request.py in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 
    643         return response

~/anaconda3/lib/python3.7/urllib/request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

~/anaconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

~/anaconda3/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Bad Request

Can anyone help me fix this?

Thanks for your time

Try not using oauth

Try not using oauth

It seems to be working now without oauth. Thanks