3

I've been going in a few loops through deprecated documentation and outdated Stackoverflow posts on this problem.

What I want to achieve: User provides my system the Shareable link (The one with _usp=sharing_ within the URL). When my system has this, it downloads the document via the Google Docs API. I don't want the user to have to go through an authorization screen because I want my application to only see the single shared document URL

Specifically, I want the results of the Google Docs API get function, which provides the document in its raw form, structure and all.

The Google Docs API suggests I can authorize using an API key but I have not been successful. The API complains that my request is missing credentials. The call in its simplest form looks something like so:

service = build('docs', 'v1', developerKey={key from dashboard})
query = service.documents.get(documentId={my doc id from shared url})
response = query.execute()

Other posts have pointed to the Google Drive API and its export function but this provides rendered formats rather than that pure JSON output I'm looking for.

Hoping someone who's bashed their head against the Google Docs API previously can help with this one :)

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Bartek
  • 15,269
  • 2
  • 58
  • 65

1 Answers1

2

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.

  1. On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
  2. Select "Me" for "Execute the app as:".
    • By this, the script is run as the owner.
  3. 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".
  4. Click "Deploy" button as new "Project version".
  5. Automatically open a dialog box of "Authorization required".
    1. Click "Review Permissions".
    2. Select own account.
    3. Click "Advanced" at "This app isn't verified".
    4. Click "Go to ### project name ###(unsafe)"
    5. Click "Allow" button.
  6. Click "OK".
  7. 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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165