question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PRAW4 - 403 on login with LMGTFY Bot Example

See original GitHub issue

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()

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

10reactions
demarcuswcommented, Jan 25, 2017

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

1reaction
oohwoohcommented, Sep 11, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Error 403 for Search Engine Bots - WordPress.org
When I analyzed all the pages response code for search engine bots then it responded with Error 403 for all the pages I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found