Forums

Discord self bot using requests, Unauthorized 401 / simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Hello, the script is working on VS code, but when i run it on pythonanywhere it show me this error, what i'm trying to do is retrieving a json sent in the chat via discord API (not websocket)

Traceback (most recent call last):
      File "/home/ScriptCordBOT1/mysite/PY_RQ_VARIABLE_CH.py", line 377, in <module>
        tmp = get_commands()
      File "/home/ScriptCordBOT1/mysite/PY_RQ_VARIABLE_CH.py", line 56, in get_commands
        resp_conv = requests.get(file_url, headers=header).json()
      File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 900, in json
        return complexjson.loads(self.text, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/simplejson/__init__.py", line 525, in loads
        return _default_decoder.decode(s)
      File "/usr/local/lib/python3.9/site-packages/simplejson/decoder.py", line 370, in decode
        obj, end = self.raw_decode(s)
      File "/usr/local/lib/python3.9/site-packages/simplejson/decoder.py", line 400, in raw_decode
        return self.scan_once(s, idx=_w(s, idx).end())
    simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When i try to print the response it return this

<Response [401]>

apparently cdn.discordapp.com is whitelisted so i don't understand why it return Unauthorized 401 error, btw i use the same account token for a ton of other action in the same server/channel so it's not a problem in the header authorization

Look at the content of the returned response to see what error the site is returning.

b"<?xml version='1.0' encoding='UTF-8'?><Error><Code>AuthenticationRequired</Code><Message>Authentication required.</Message></Error>"

but it's totaly working while testing on VSCode consoles, so it's a bit weird

Then you must be missing something that provides the authentication from the code on PythonAnywhere.

nothing is missing, like i said it's perfectly working on VS code, and discord authentification work with only the token when using requests, i think it's a problem with retreving json file via cdn.discordapp on pyanywhere side

I tried like this:

            header = {
                "authorization": bot_token
            }
            resp_conv = requests.get(file_url, headers=header)
            resp_conv.encoding = "utf-8"
            data_conv = resp_conv.text

Instaed of like thsis:

            header = {
                "authorization": bot_token
            }
            resp_conv = requests.get(file_url, headers=header).json()
            print(resp_conv)

and it still not working same error:

<?xml version='1.0' encoding='UTF-8'?><Error><Code>AuthenticationRequired</Code><Message>Authentication required.</Message></Error>

while token is 100% right, and it's 100% working on vs code console

ok , funny things is that is tested out, and you don't even need token authorization to retrieve the json, but on pyanywhere it keep saying

<?xml version='1.0' encoding='UTF-8'?><Error><Code>AuthenticationRequired</Code><Message>Authentication required.</Message></Error>

ok, if anyone have the same problem one day, i found the solution, you just need to not specify the headers, only the url, idk why it was working on my vscode and not here

Glad you got that working.