praw: PRAW4 - 403 on login with LMGTFY Bot Example

Issue Description

When I attempt to run the LMGTFY example bot from the docs, I’m getting a 403 Forbidden.

Stack Trace

Traceback (most recent call last):
  File "test.py", line 37, in <module>
    main()
  File "test.py", line 15, in main
    for submission in subreddit.stream.submissions():
  File "/usr/local/lib/python2.7/dist-packages/praw/models/util.py", line 40, in stream_generator
    limit=limit, params={'before': before_fullname}))):
  File "/usr/local/lib/python2.7/dist-packages/praw/models/listing/generator.py", line 70, in next
    return self.__next__()
  File "/usr/local/lib/python2.7/dist-packages/praw/models/listing/generator.py", line 43, in __next__
    self._next_batch()
  File "/usr/local/lib/python2.7/dist-packages/praw/models/listing/generator.py", line 53, in _next_batch
    self._listing = self._reddit.get(self.url, params=self.params)
  File "/usr/local/lib/python2.7/dist-packages/praw/reddit.py", line 207, in get
    data = self.request('GET', path, params=params)
  File "/usr/local/lib/python2.7/dist-packages/praw/reddit.py", line 258, in request
    return self._core.request(method, path, params=params, data=data)
  File "/usr/local/lib/python2.7/dist-packages/prawcore/sessions.py", line 119, in request
    params)
  File "/usr/local/lib/python2.7/dist-packages/prawcore/sessions.py", line 73, in _request_with_retries
    raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Forbidden: received 403 HTTP response

System Information

PRAW Version: praw-4.0.0b17
Python Version: 2.7.12
Operating System:  Linux Mint 18 Cinnamon (Ubuntu 16.04 LTS)

Code

from urllib import quote_plus

import praw

QUESTIONS = ['what is', 'who is', 'what are']
REPLY_TEMPLATE = '[Let me google that for you](http://lmgtfy.com/?q={})'


def main():
    reddit = praw.Reddit(user_agent='This is a thing that gives you stuff by /u/Zetaphor',
                         client_id='My Client ID', client_secret="My Client Secret",
                         username='My Username', password='My Password')

    subreddit = reddit.subreddit('AskReddit')
    for submission in subreddit.stream.submissions():
        process_submission(submission)


def process_submission(submission):
    # Ignore titles with more than 10 words as they probably are not simple
    # questions.
    if len(submission.title.split()) > 10:
        return

    normalized_title = submission.title.lower()
    for question_phrase in QUESTIONS:
        if question_phrase in normalized_title:
            url_title = quote_plus(submission.title)
            reply_text = REPLY_TEMPLATE.format(url_title)
            print('Replying to: {}'.format(submission.title))
            submission.reply(reply_text)
            # A reply has been made so do not attempt to match other phrases.
            break


if __name__ == '__main__':
    main()

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

I’m late to the party, but I may be able to offer some help if anyone is still having this issue. I ran into the exact same problem as, @Zetaphor , and tweaking my User Agent fixed it for me.

Here’s what my User Agent was previously:

UA = 'Bot by /u/[name here]'

r = praw.Reddit('bot', user_agent=UA)

This was giving me a 403 forbidden.

After changing the user agent to include what my app is actually called:

UA = 'Over 9000 Bot by /u/[name here]'

r = praw.Reddit('bot', user_agent=UA)

No more 403 error and my personal script can freely crawl Reddit. Hope this helps

@demarcusw thanks a million! Been debugging for half an hour and this solved my problem!