1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] gspread API 502 Server Error when updating a Google Sheet with Python

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024 às 08:52.

  1. Stack

    Stack Membro Participativo

    I'm pretty new to this stuff, so I'm going to provide as much information as possible just in case it is relevant.

    In VSCode (1.93.1), I created a Python script which makes updates to a Google Sheets file using the gspread API. The script was working for 6 months with no issues, but it suddenly started throwing a 502 server error even though I hadn't made any changes to the code, or to the modules, or to the python version, or to anything at all. I use python3 when running the program. This error has persisted for two weeks.

    <p><b>502.</b> <ins>That’s an error.</ins>
    <p>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds. <ins>That’s all we know.</ins>


    The script needs to iterate over 900 elements of an array, and the error occurs at different points each time. On average, it gets through about 100 elements before the error occurs, but it's stopped as soon as the 3rd element and as far as the 351st element, so the timing of the error seems random. In case the gspread API reduced their rate limits, I used the time module to pause the program for 10 seconds on each iteration, and it still ended up with a 502 error on the 152nd iteration.

    I'm using Windows 11 Pro and Ubuntu (WSL).

    I can provide some of the code if necessary, but since the program is exiting at different points each time, it seems like the issue isn't with the code, especially since the code worked for 6 months before I started getting this error.

    I also tried recreating the script from scratch in PyCharm, and I'm able to import gspread and run the program, but now it gives a JSONDecodeError, which also occurs at seemingly random times. I've run the program on PyCharm twice, and the first time stopped at the 24th element of the array, and the second time it stopped at the 136th element (without changing anything).

    In the file paths below, I've replaced the user's name and the file names with "..."

    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\requests\models.py", line 974, in json
    return complexjson.loads(self.text, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Users\...\PycharmProjects\...\...\file.py", line 46, in <module>
    if sheet.find(new_sku):
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\gspread\worksheet.py", line 2326, in find
    return next(self._finder(filter, query, case_sensitive, in_row, in_column))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\gspread\worksheet.py", line 2243, in _finder
    data = self.client.values_get(
    ^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\gspread\http_client.py", line 236, in values_get
    r = self.request("get", url, params=params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\gspread\http_client.py", line 128, in request
    raise APIError(response)
    ^^^^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\gspread\exceptions.py", line 45, in __init__
    self.error: Mapping[str, Any] = response.json()["error"]
    ^^^^^^^^^^^^^^^
    File "C:\Users\...\PycharmProjects\...\...\.venv\Lib\site-packages\requests\models.py", line 978, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
    requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


    When it says another exception occurred on line 46 if sheet.find(new_sku):, I don't think the issue is with that code because that line of code gets executed on every element of the array, so if I run the script and it stops at the 200th element, then I run it again and it stops at the 50th element, why did it get past the 50th element the first time?

    I'm completely confused about why this program suddenly stopped working. If anyone has an idea, I would really appreciate it.

    Continue reading...

Compartilhe esta Página