我故意使用PRAW使用错误的凭据对reddit API进行身份验证。我可以在控制台上看到“收到401 HTTP响应”错误。
Traceback (most recent call last):
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/bob/Sites/django/myproj/src/blog/views.py", line 246, in syncMyPosts
thisimage.remove_category = submission.removed_by_category
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/base.py", line 33, in __getattr__
self._fetch()
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 591, in _fetch
data = self._fetch_data()
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 588, in _fetch_data
return self._reddit.request("GET", path, params)
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/reddit.py", line 726, in request
return self._core.request(
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 329, in request
return self._request_with_retries(
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 227, in _request_with_retries
response, saved_exception = self._make_request(
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 185, in _make_request
response = self._rate_limiter.call(
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/rate_limit.py", line 35, in call
kwargs["headers"] = set_header_callback()
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 282, in _set_header_callback
self._authorizer.refresh()
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 349, in refresh
self._request_token(
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 149, in _request_token
response = self._authenticator._post(url, **data)
File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 32, in _post
raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response
我宁愿捕获此错误/异常并显示友好信息。很难理解如何使用try catch异常。
Python和Django的新手。
提前感谢
这里是一个简短的示例,使用Python的try
and except
完成了您想要的。我们导入try
,因为这是我们要捕获的。
except
我们用ResponseException
和import praw
from prawcore.exceptions import ResponseException
reddit = praw.Reddit(
username="wrong_username",
password="wrong password",
client_id="bad client ID",
client_secret="bad secret",
user_agent="failing authentication",
)
try:
print("Authenticated as {}".format(reddit.user.me()))
except ResponseException:
print("Something went wrong during authentication")
块包围了将引发异常(在这种情况下为reddit.user.me()
的代码,以便我们可以指定发生异常时的替代行为。