Getting JSON files from Google drive

Hi,
I’m having trouble reading JSON files from my Google drive.
On the Google side, I have an apps script that creates several .json files with data from the spreadsheets, and then creates a .csv file with the URLs to those json files. The json files are created using DriveApp.createFile() so they are text files, not Google Docs. I then use either getUrl() or getDownloadUrl() to obtain the URL of the json file and add it to the list in the csv file.
On the Hugo side, I use getCSV to read the csv file, find the URL to the json file that corresponds to the current page, and then use getJSON to get to the data. This gets an error, detailed below.
When I copy the json files to the Hugo data directory, it works fine - the file is parsed correctly, and the data is displayed ok. However when I use getJSON to read the files directly from Google drive, it does not work.

When I use getUrl() on the Google side, the URLs looks like this:
https://drive.google.com/file/d/<file-id>/view?usp=drivesdk
When I run Hugo, I get the following error for every page:
Failed to get JSON resourse "<path...>": Invalid character '<' looking for beginning of value.

When I use getDownloadUrl() on the Google side, I get URLs that look like this:
https://doc-08-9g-docs.googleusercontent.com/docs/securesc/<very-long-code>?e=download?gd=true
When I run Hugo, I get the following error for every page:
Failed to get JSON resource "<path...>": Failed to retrieve remote file: Unauthorized.

I do apologize for not providing sample code. Because of the integration with Google drive, it is challenging. It is quite a big project. Sorry.
Thanks,
Yuval.

1 Like

work around: rsync the files to your local drive, ex.: /assets

Your URL to the file in your drive is wrong. Go to the share option (in drive) and copy the link where it says something like “read access for everyone with this link”. Then it should work.

1 Like

Thanks, but I’m afraid this is not practical in my case.
I am developing this for someone else, who may have a different setup/

Thanks @davidsneighbour. I need this to work programmatically - not to get the link by hand, but rather have the Google apps script get it and put it in the .csv file. There are quite a few JSON files, and getting the links manually every time the data changes is not practical.

I don’t think the links would change once they have been marked as anyone with the link can view, as long as you don’t delete the file, your changes should show up. Otherwise using this command (or something similar) might work.
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW;

But the error you are posting says “Unauthorized” - So this is your first point of research. If you get them programmatically your API-Key is not authorized to access the file in question. Maybe the key you created has only read rights and the way you access the files asks for writing rights. I am not too much versed in the Google API but this should be the first point of research for that specific error code.

This is an 11 month old thread and not exactly on topic.

However I have spent some time looking at various APIs this week, including the Google Drive one.

In the latest iteration (v3) of the Google Drive API, there is no way for anyone to access the contents of a public JSON file without authentication.

An API key is required (you can get one here) and then one can use the following endpoint:

https://www.googleapis.com/drive/v3/files/<file-ID>?alt=media&key=<API key>

ref

By passing the above endpoint to Hugo’s getJSON function it is possible to access the contents of a public JSON from Google Drive and process them in a project’s templates, without using any third party commercial service.

Also if one knows how to automate things, it is very much possible to use a Google Sheet as the back end for a Hugo project (no need for the complicated Google Drive CMS).

There is a pretty useful extension to output a Google sheet to JSON in Drive available over here. I tested it and it generates valid JSON, including nested JSON arrays and it supports overwriting the same file in Drive.

So yes. It is possible to do some amazing things with Hugo for free.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.