user6819496
user6819496

Reputation:

"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

Upvotes: 11

Views: 13866

Answers (6)

tobahhh
tobahhh

Reputation: 381

This error seems to pop up sometimes if you're using a Reddit account created using OAuth with a 3rd party service ("Sign in with Google" or "Sign in with Apple").

You may need to create a Reddit password (separate from your 3rd party service) or disconnect those services entirely. Both can be done on the Reddit website here.

Upvotes: 0

user1465073
user1465073

Reputation: 325

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

Upvotes: 1

Abd Alhade Ahmad
Abd Alhade Ahmad

Reputation: 21

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

Upvotes: 2

TheColorRedYT
TheColorRedYT

Reputation: 1

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

Upvotes: 0

Dr-Bracket
Dr-Bracket

Reputation: 5574

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.

Upvotes: 3

zEaK47
zEaK47

Reputation: 134

Double check your credentials as this note says.

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

Upvotes: 11

Related Questions