"invalid_grant error processing request" while making a reddit bot

Question:

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:ProgrammingPhytonlibsite-packagesprawmodelsredditbase.py", line 31, in __getattr__
    self._fetch()
  File "D:ProgrammingPhytonlibsite-packagesprawmodelsredditbase.py", line 66, in _fetch
    params=self._info_params)
  File "D:ProgrammingPhytonlibsite-packagesprawreddit.py", line 367, in get
    data = self.request('GET', path, params=params)
  File "D:ProgrammingPhytonlibsite-packagesprawreddit.py", line 451, in request
    params=params)
  File "D:ProgrammingPhytonlibsite-packagesprawcoresessions.py", line 174, in request
    params=params, url=url)
  File "D:ProgrammingPhytonlibsite-packagesprawcoresessions.py", line 108, in _request_with_retries
    data, files, json, method, params, retries, url)
  File "D:ProgrammingPhytonlibsite-packagesprawcoresessions.py", line 93, in _make_request
    params=params)
  File "D:ProgrammingPhytonlibsite-packagesprawcorerate_limit.py", line 32, in call
    kwargs['headers'] = set_header_callback()
  File "D:ProgrammingPhytonlibsite-packagesprawcoresessions.py", line 134, in _set_header_callback
    self._authorizer.refresh()
  File "D:ProgrammingPhytonlibsite-packagesprawcoreauth.py", line 328, in refresh
    password=self._password)
  File "D:ProgrammingPhytonlibsite-packagesprawcoreauth.py", line 142, in _request_token
    payload.get('error_description'))
prawcore.exceptions.OAuthException: invalid_grant error processing request
Asked By: user6819496

||

Answers:

Double check your credentials as this note says.

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

Answered By: zEaK47

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.

Answered By: Dr-Bracket

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

Answered By: TheColorRedYT

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

Answered By: Abd Alhade Ahmad

I encountered this error today, the reason being is that I enabled two-factor authentication with my Reddit account.

Removing the two-factor authentication made my bot/ app progress again.

You can also address it with the suggestion above: https://stackoverflow.com/a/71905094/1465073

Answered By: user1465073
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.