0

Pardon me, I'm not well-versed with curl, but I am trying to download a Google Drive image using it. The file downloads as an HTML, rather than as a jpg. I am calling the curl via Applescript shell script wrapped in an Extendscript app.doScript command, but I tried also from the Terminal and received the same file. I followed these instructions: https://stackoverflow.com/a/48133140/1810714

Here is the full Extendscript code I am working with.

var assetFolderPath = Folder.selectDialog();
var fileName = "/test.jpg";
var front = "https://drive.google.com/uc?export=download&id=";
var root = "google-drive-id#"; //the real id is here instead
var exporturl = front + root;
var ff = File(assetFolderPath + fileName);
var curlCommand = "\"curl -L -o '" + ff.fsName + "' " + "'" + exporturl + "'";
var asCode = 'do shell script ' + curlCommand + '"';
app.doScript(asCode, ScriptLanguage.APPLESCRIPT_LANGUAGE);

Thanks in advance.

iPadre
  • 23
  • 4

1 Answers1

1

How to download a file with the Drive API with curl

You seem to be trying to download a file with the files.export endpoint, however in the docs it says:

Exports a Google Doc to the requested MIME type and returns the exported content.

That is, the export endpoint only works for Google Doc types, i.e. Sheets, Docs, Slides, etc.

For all other types of file, what you need to use is files.get with an ?alt=media at the end of the URL. For this you need the file ID and your oauth access token.

The curl command in bash would look something like this:

curl \
  "https://www.googleapis.com/drive/v3/files/${FILE_ID}?alt=media" \
  --header "Authorization: Bearer ${ACCESS_TOKEN}" \
  --header 'Accept: application/json' \
  --compressed -o $OUTPUT_PATH

Replace the ${} variables with the file id, the oauth access token, and the output path.

For instance:

curl \
  "https://www.googleapis.com/drive/v3/files/xxxxxxx?alt=media" \
  --header "Authorization: Bearer xxxxxxx" \
  --header 'Accept: application/json' \
  --compressed -o image.jpg

Assuming the ID that is chosen is a .jpg file, then in the folder where this is run, it will create a image.jpg image file.

You can find more info in the guide.

Reference

iansedano
  • 6,169
  • 2
  • 12
  • 24
  • Thank you. That makes sense as to why I was barking up the wrong tree. Of course, it unfurls another problem in that Extendscript doesn't exactly have good HTTPS socket support to obtain an Oauth Access Token, but I'll try to hammer away at that. – iPadre Aug 18 '21 at 04:45
  • You can obtain Oauth access tokens with just bash and curl, not sure how you need it to work, but that is an option in case you need to go there [example](https://gist.github.com/iansedano/e0b259ab9c63ebddd22658f697026c19) – iansedano Aug 18 '21 at 07:06