Mission 110-3: Challenge: Working with the reddit API; unable to access data; Authentication failure?

The code appears to work fine in the dataquest console but doesn’t work when I try to reproduce it in the Jupyter notebook.

Screen Link:

My Code:

headers = {"Authorization": "bearer 13426216-4U1ckno9J5AiK72VRbpEeBaMSKk", "User-Agent": "Dataquest/1.0"}
params = {"t": "day"}
response = requests.get("https://oauth.reddit.com/r/python/top", headers=headers, params=params)

python_top = response.json()

print(python_top.keys())
python_top['data'].keys()
data_child_lst=python_top['data']['children']
print(type(data_child_lst))
data_child_lst[0]['data']['id']
data_child_lst[0]['data']['ups']

What I expected to happen:
display of the result that appears in the console:
dict_keys([‘kind’, ‘data’])
<class ‘list’>
53

What actually happened:

JSONDecodeError                           Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\simplejson\__init__.py:525, in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw)
    521 if (cls is None and encoding is None and object_hook is None and
    522         parse_int is None and parse_float is None and
    523         parse_constant is None and object_pairs_hook is None
    524         and not use_decimal and not kw):
--> 525     return _default_decoder.decode(s)
    526 if cls is None:

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\simplejson\decoder.py:370, in JSONDecoder.decode(self, s, _w, _PY3)
    369     s = str(s, self.encoding)
--> 370 obj, end = self.raw_decode(s)
    371 end = _w(s, end).end()

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\simplejson\decoder.py:400, in JSONDecoder.raw_decode(self, s, idx, _w, _PY3)
    399         idx += 3
--> 400 return self.scan_once(s, idx=_w(s, idx).end())

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Input In [5], in <cell line: 5>()
      2 params = {"t": "day"}
      3 response = requests.get("https://oauth.reddit.com/r/python/top", headers=headers, params=params)
----> 5 python_top = response.json()
      7 print(python_top.keys())
      8 python_top['data'].keys()

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

what does the error message mean and how to fix it?

Thank you

It seems the json command is unable to read the data; could someone kindly guide me how to address this issue?
its preventing me from working on this course and delaying progress.

On further review it seems there is an authentication failure:

response.status_code

yields the result:
“401”
indicating
401 — The server thinks you’re not authenticated. This happens when you don’t send the right credentials to access an API.\

Was anyone able to replicate the results outside of the dq console?

Thanks

@hanqi I have been looking at the Q&A forum here and you have given some very detailed and insightful answers;
I was hoping you could give me some pointers as well.

Throughout this course about APIs, I have been unable to reproduce results outside of the dataquest console;

The issue seems to be an authentication failure: the status_code method yields code 401.

the response.text will give a result like:

response.text

Output:

<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><title>Python: page not found</title><meta name="keywords" content=" reddit, reddit.com, vote, comment, submit " /><meta name="description" content="News about the programming language Python. If you have something to teach others post here. If you have questions or are a newbie use r/learnpython." /><meta name="referrer" content="origin-when-cross-origin"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link type="application/opensearchdescription+xml" rel="search" href="/static/opensearch.xml"/><link rel="canonical" href="https://www.reddit.com/r/python/comments/4b7w9u/" /><meta name="viewport" content="width=1024"><link rel="dns-prefetch" href="//out.reddit.com"><link rel="preconnect" href="//out.reddit.com"><link rel="apple-touch-icon" sizes="57x57" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-57x57.png" /><link rel="apple-touch-icon" sizes="60x60" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-60x60.png" /><link rel="apple-touch-icon" sizes="72x72" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-72x72.png" /><link rel="apple-touch-icon" sizes="76x76" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-76x76.png" /><link rel="apple-touch-icon" sizes="114x114" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-114x114.png" /><link rel="apple-touch-icon" sizes="120x120" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-120x120.png" /><link rel="apple-touch-icon" sizes="144x144" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-144x144.png" /><link rel="apple-touch-icon" sizes="152x152" href="//www.redditstatic.com/desktop2x/img/favicon/apple-icon-152x152.png" /> ........

At first I thought the results were being submitted in html but review of this string data shows it is just the website html data not the real stored data about the reddit posts.

Please let me know how to get the information needed and how to approach such problems.

Thanks

Could anybody at least advise whether the authentication is only designed to work in the console.