I believe your goal as follows.
- You want to retrieve the Google Document object as JSON data using the get method of Google Docs API.
- You want to achieve this using API key.
- You want to make users download the Google Document object using a single URL.
- From
I want my application to only see the single shared document URL
.
For this, how about this answer?
Issue and workaround:
In the current stage, it seems that the get method of Docs API cannot be used with the API key, even when the Google Document is publicly shared. When the get method of Docs API is requested to the publicly shared Google Document with the API key, the following value is returned. (In this case, it has already been confirmed that the API key can be used for Drive API.)
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
From this result, it is found that the API key cannot be used for this situation. In order to avoid this, it can consider about the use of the service account. But in the current stage, the access token cannot be used as the query parameter. Ref So it is required to think of other workaround.
So, as another workaround, I would like to propose to use Web Apps created by Google Apps Script for achieving your goal. In this case, the Web Apps is used as an API.
Usage:
Please do the following flow.
1. Create new project of Google Apps Script.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
2. Prepare script.
Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Docs API at Advanced Google services. This script is for the Web Apps.
function doGet(e) {
const key = "samplekey"; // Please set the key for using this Web Apps.
const k = e.parameter.key;
const documentId = e.parameter.documentId;
if (k === key) {
const obj = Docs.Documents.get(documentId, {fields: "*"});
return ContentService.createTextOutput(JSON.stringify(obj)).setMimeType(ContentService.MimeType.JSON);
}
return ContentService.createTextOutput(JSON.stringify({error: "Wrong key."})).setMimeType(ContentService.MimeType.JSON);
}
- In this case, the GET method is used.
3. Deploy Web Apps.
- On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
- Select "Me" for "Execute the app as:".
- By this, the script is run as the owner.
- Select "Anyone, even anonymous" for "Who has access to the app:".
- In this case, no access token is required to be request. I think that I recommend this setting for your goal.
- Of course, you can also use the access token. At that time, please set this to "Anyone".
- Click "Deploy" button as new "Project version".
- Automatically open a dialog box of "Authorization required".
- Click "Review Permissions".
- Select own account.
- Click "Advanced" at "This app isn't verified".
- Click "Go to ### project name ###(unsafe)"
- Click "Allow" button.
- Click "OK".
- Copy the URL of Web Apps. It's like
https://script.google.com/macros/s/###/exec
.
- When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
4. Run the function using Web Apps.
This is a sample python script for requesting Web Apps. Please set your Web Apps URL and documentId.
import requests
document_id = '###' # Please set the Document ID.
key = 'samplekey' # Please set the key for using Web Apps.
url = 'https://script.google.com/macros/s/###/exec?documentId=' + document_id + '&key=' + key
res = requests.get(url)
print(res.text)
- In this case, the GET method is used at Web Apps side. So you can also directly access to the above URL using your browser.
Note:
- When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
References: