Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: "Precondition check failed." #73

Open
safo-bora opened this issue Jun 21, 2022 · 0 comments
Open

Exception: "Precondition check failed." #73

safo-bora opened this issue Jun 21, 2022 · 0 comments

Comments

@safo-bora
Copy link

FOUND:
The error appears occasionally (1-2 times from 10)

HERE IS SOURCE:

from simplegmail import Gmail
from simplegmail.query import construct_query

def get_verification_code_from_inbox():
    print(f"EMAILS RECIPIENT-> {EMAIL_RECIPIENT}")

    email = Gmail()
    query_params = {
      "newer_than": (1, "day"),
      "recipient": EMAIL_RECIPIENT
    }

    print(f"Start verify INBOX email folder! [Recipient: {EMAIL_RECIPIENT}]")

    messages = email.get_unread_messages(query=construct_query(query_params))
    print(f"EMAILS FOUND -> {len(messages)}")

    datetime_now = datetime.datetime.now()
    print(f"TIME NOW -> {datetime_now}")

    for message in messages:
        datetime_target = datetime.datetime.strptime(message.date.strip(), '%Y-%m-%d %H:%M:%S%z')
        time_elapsed = datetime_now.replace(tzinfo=None) - datetime_target.replace(tzinfo=None)

        if time_elapsed < datetime.timedelta(minutes=5):
            print("EMAIL WAS FOUND!")

            message.mark_as_read()

            print("=" * 100)
            print("To: " + message.recipient)
            print("From: " + message.sender)
            print("Subject: " + message.subject)
            print("Message Body: " + message.plain)
            print(f"Letter was received: {time_elapsed} seconds ago")
            print("=" * 100)

The Exception:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 627, in thread_download_batch
    message_lists[thread_num] = [
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 628, in <listcomp>
    gmail._build_message_from_ref(
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 690, in _build_message_from_ref
    user_labels = {x.id: x for x in self.list_labels(user_id=user_id)}
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 568, in list_labels
    raise error
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 562, in list_labels
    res = self.service.users().labels().list(
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://gmail.googleapis.com/gmail/v1/users/me/labels?alt=json returned "Precondition check failed.". Details: "[{'message': 'Precondition check failed.', 'domain': 'global', 'reason': 'failedPrecondition'}]">
Traceback (most recent call last):
  File "reading_email.py", line 105, in <module>
    code = get_verification_code_from_inbox()
  File "reading_email.py", line 62, in get_verification_code_from_inbox
    messages = email.get_unread_messages(query=construct_query(query_params))
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 316, in get_unread_messages
    return self.get_messages(user_id, labels, query, attachments,
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 534, in get_messages
    return self._get_messages_from_refs(user_id, message_refs,
  File "/usr/local/lib/python3.8/site-packages/simplegmail/gmail.py", line 645, in _get_messages_from_refs
    return sum(message_lists, [])
TypeError: can only concatenate list (not "NoneType") to list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant