0

When using Google Drive API to export online doc in G Suite, we found that under some conditions Google will respond in HTML format instead of known json format.

Condition1 - sharing online doc to an user who will be suspended later

  1. Create an online doc under userA's MyDrive.
  2. Share this online doc to userB. (userB then add it to its MyDrive)
  3. Get userB's access token and try to export this online doc. (up to now, everything works fine, and the online doc can be successfully exported.)
  4. Suspend userB by admin in G Suite.
  5. Use the access token got in step 3 to export the same online doc. This time, Google will response http code 403 and respond HTML body like:

    <!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"><link rel="shortcut icon" href="//ssl.gstatic.com/docs/common/drive_favicon1.ico"><title>Google Drive - Access Denied</title><link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css"><style>/* Copyright 2017 Google Inc. All Rights Reserved. */ .goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}</style><style>/* Copyright 2017 Google Inc. All Rights Reserved. */

In above HTML tags, we can find Google Drive - Access Denied in <title> tag. In fact, we expect to get the response body in json format. For example:

{
    "error": {
    "errors": [
        {
            "domain": "global",
            "reason": "insufficientFilePermissions",
            "message": "The user does not have sufficient permissions for file {fileId}."
        }
    ],
    "code": 403,
    "message": "The user does not have sufficient permissions for file {fileId}."
    }
}

For normal files (sharing to suspended user), Google will respond with json formatted body.

Condition2 - export online doc using invalid access token

  1. Create an online doc in userC's MyDrive.
  2. Export this online doc using invalid credentials. For example:

    curl -H "Authorization: Bearer INVALID_TOKEN" "https://docs.google.com/feeds/download/documents/export/Export?id=[FILE_ID]&exportFormat=docx"

  3. Google will respond with http code 401 and body:

    <HTML> <HEAD> <TITLE>Unauthorized</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Unauthorized</H1> <H2>Error 401</H2> </BODY> </HTML>

  4. However, if using invalid token to download normal files, Google will respond HTTP code 401 and body:

    {
        "error": {
        "errors": [
            {
            "domain": "global",
            "reason": "authError",
            "message": "Invalid Credentials",
            "locationType": "header",
            "location": "Authorization",
            }
        ],
        "code": 401,
        "message": "Invalid Credentials"
        }
    }
    

We don't know why Google will respond in those HTML body instead of json in above two conditions. Please help if someone knows why, thanks!

Note: we use curl to send Drive API request. (without using any SDK)

Guan
  • 63
  • 7
  • I am not sure how your code look like, good if you can share it here. But maybe you can find these [SO post](https://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest) helpful. Have you already tried the suggestions here? Also you can follow the [best practices](https://developers.google.com/drive/v3/web/practices) that the documentation provided. – MαπμQμαπkγVπ.0 Nov 15 '17 at 11:05
  • I think the problem we post here is about the response from Google when using `export` link to download online doc. We also provide the reproducible steps which will lead to these response as anyone can verify. We are wondering why the provided two conditions will lead to `HTML` response instead of `JSON`. Is it the case that error occurs when using `export` link will always respond in `HTML` format instead of `JSON`? – Guan Nov 16 '17 at 00:50

0 Answers0