Skip to content

Python error while trying to download files #2

@jak83A

Description

@jak83A

On the GAS side
I have commented out the "upload" (see syntax but in issue before) and the "delete" section.
Then I have also commented out the parameters part, as it seems to be not needed at this stage:

function doPost(e) {
  // if (e.parameters.method == "download") {
  try {
    return (function(id){
      var file = DriveApp.getFileById(id);
      return ContentService
            .createTextOutput(JSON.stringify({
              size: file.getBlob().getBytes(),
              name: file.getName(),
              result: file.getName() + " (" + file.getBlob().getContentType() + ")"
            }))
            .setMimeType(ContentService.MimeType.JSON);
    })(e.parameters.id);
  } catch(err) {
    return ContentService.createTextOutput(JSON.stringify({
              result: err.message
            }))
            .setMimeType(ContentService.MimeType.JSON);
  }
}

I have deployed the web app with the permissions: execute as me, by anyone in our organization.

On the Python side
I have imported the following three modules as in your code. I
Then i have copied the web app's URL and inserted it as a global variable into my script.
Finally i

import mimetypes
import numpy as np
import requests

web_app_url = <url of my web app>

def GetFilesFromWebApp(file_id):
    r = requests.post(
        web_app_url,
        data={"id": file_id}
        # data={"method": "download", "id": file_id}
    )
    if len(r.json()["name"]) > 0:
        with open(r.json()["name"], "bw") as f:
            f.write(np.array(r.json()["size"], dtype=np.uint8))
    return r.json()["result"]

GetFilesFromWebApp(<my file id>)

Then i get the following error:

Traceback (most recent call last):
  File "<path to my script>", line 194, in <module>
    GetFilesFromWebApp("my file id>)
  File "<path to my script>", line 189, in GetFilesFromWebApp
    if len(r.json()["name"]) > 0:
  File "/usr/lib/python3/dist-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/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)

Process finished with exit code 1

Please help, as your solution seems to be exactly what i need.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions