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
- Create an online doc under userA's MyDrive.
- Share this online doc to userB. (userB then add it to its MyDrive)
- 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.)
- Suspend userB by admin in G Suite.
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
- Create an online doc in userC's MyDrive.
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"
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>
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)