kootenpv/yagmail

Calling call_refresh_token results in HTTP 400 Error: Bad Request

Closed this issue · 2 comments

First, just wanted to say thanks for such an awesome library. Now, I am obtaining an HTTP Error 400: Bad Request when attempting to send an email. I thought that the issue was being caused by an unexpired token perhaps. I could not quite track down exactly why this error was happening. But I run this scraper ~ once a week and after about a couple of months, this error started popping up.

Both the error and the relevant code that is producing the error can be found below.

Traceback (most recent call last):
  File "scraper.py", line 33, in main
    contents=updated_trials,
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/sender.py", line 147, in send
    self.login()
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/sender.py", line 244, in login
    self._login_oauth2(self.credentials)
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/sender.py", line 223, in _login_oauth2
    auth_string = self.get_oauth_string(self.user, oauth2_info)
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/sender.py", line 213, in get_oauth_string
    return get_oauth_string(user, oauth2_info)
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/oauth2.py", line 98, in get_oauth_string
    access_token, expires_in = refresh_authorization(**oauth2_info)
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/oauth2.py", line 93, in refresh_authorization
    response = call_refresh_token(google_client_id, google_client_secret, google_refresh_token)
  File "/home/mattwilliams1760/.local/lib/python3.5/site-packages/yagmail/oauth2.py", line 73, in call_refresh_token
    response = urlopen(request_url, encoded_params).read().decode('UTF-8')
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

Here is the relevant code in my script using the pattern you describe in the docs.

yag = yagmail.SMTP("email", oauth2_file="/home/mattwilliams1760/oauth2_creds.json") 
                yag.send(
                    to=["email", "email"],
                    subject="subject",
                )

I have all credentials in the oauth2_creds.json file in the following format:

{"email_address": "email address", "google_client_id": "client id", "google_refresh_token": "google refresh token", "google_client_secret": "google client secret"}

Any help would be greatly appreciated in tracking down what is going on. Please let me know if anything else is needed to help understand what is going on, I am still a bit of a beginner. My entire repo can be found here: https://github.com/asharma13524/clinical-trials-scraper

Yea I am not sure, I think OAuth2 possibly might not work anymore. I'd recommend switching to application specific passwords - this is my current way.

Hope it's useful to you!

Thank you very much. This worked!