'"invalid_grant error processing request" while making a reddit bot

Im making a reddit bot that replies something to a specific comment.

But Im getting this error : invalid_grant error processing request

and I can't find the solution.

here is my code, Im using Python.

import praw
import time
import config

def login():
    r = praw.Reddit(user_agent = "test bot",
                username = config.username,
                password = config.password,
                client_id = config.client_id,
                client_secret = config.client_secret)
    print("logged in")
    return r


cache = []

def run_bot(r):
    subreddit = r.subreddit("Test")
    comments = subreddit.get_comments(limit=25)
    for comment in comments:
        comment_text = comment.body.lower()
        if "xD" in comment_text and comment.id not in cache:
            comment.reply("xD")
            cache.append(comment.id)

while True:
    r = login()
    run_bot(r)
    time.sleep(5)

traceback:

    logged in
Traceback (most recent call last):
  File "xdbot.py", line 28, in <module>
    run_bot(r)
  File "xdbot.py", line 19, in run_bot
    comments = subreddit.get_comments(limit=25)
  File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
    self._fetch()
  File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 66, in _fetch
    params=self._info_params)
  File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 367, in get
    data = self.request('GET', path, params=params)
  File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 451, in request
    params=params)
  File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 174, in request
    params=params, url=url)
  File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 108, in _request_with_retries
    data, files, json, method, params, retries, url)
  File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 93, in _make_request
    params=params)
  File "D:\Programming\Phyton\lib\site-packages\prawcore\rate_limit.py", line 32, in call
    kwargs['headers'] = set_header_callback()
  File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 134, in _set_header_callback
    self._authorizer.refresh()
  File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 328, in refresh
    password=self._password)
  File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 142, in _request_token
    payload.get('error_description'))
prawcore.exceptions.OAuthException: invalid_grant error processing request


Solution 1:[1]

Double check your credentials as this note says.

Remember that the username is your reddit's account name, not the bot's name.

Solution 2:[2]

Another possibility is your bot has been timed out for too many login attempts.

An easy way to check this by trying to log into Reddit manually and checking if you're blocked.

Solution 3:[3]

You might be using two-factor authentication with this account. You have to disable it, or pass the 2FA code with the password in one stiring separated by a colon, like this:

r = praw.Reddit(user_agent = "test bot",
    username = config.username,
    password = f"{config.password}:{2FA-Code}",
    client_id = config.client_id,
    client_secret = config.client_secret)

See this in the docs: https://praw.readthedocs.io/en/latest/getting_started/authentication.html#two-factor-authentication

Solution 4:[4]

Also, your code won't work because you use the "lower" command and there's an uppercase letter in the string you compare.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zEaK47
Solution 2
Solution 3 Abd Alhade Ahmad
Solution 4 TheColorRedYT