Search code examples
google-apps-scriptgoogle-drive-apigoogle-workspace

Google Drive Doc Fill/AppScripts


I've been working with the Google Drive API in my Java application. The goal is to have a framework for our internal AppEngine project to fill out various templates in our organization's Team Drives.

I have setup a Service Account with domain-wide permissions and have gone through the "Executing Functions using the Apps Script API" documents. I can use the Java v3 library to update a sheet to get some number information, copy the template to a new file in the Team Drive. I've written a simple AppScript function to take in the Docs File Id and a JSON string representing the token/value pairs to substitute.

Everything goes great until it's time to execute the script when I get:

{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Request contains an invalid argument.",
    "reason" : "badRequest"
  } ],
  "message" : "Request contains an invalid argument.",
  "status" : "INVALID_ARGUMENT"
}

Not sure how to debug or change this error, as I don't really get any additional info. I do see the request being made in the AppScript API and project console, but don't know how to get more information.

My code to execute is as follows:

List<Object> params = Lists.newArrayList();
    params.add( fileId );
    params.add( json );
ExecutionRequest request = new ExecutionRequest().setFunction( "main2" )
    .setParameters( params );
script().scripts().run( TEMPLATE_FILLER_SCRIPT_ID, request ).execute();

After enabling the logging to I was able to pull the entire web request. It all looks ~fine but maybe there's more here:

CONFIG: {"function":"main2","parameters":["15gXG9frqV0VrF57BjDqOe0pCHPWxVJ1ucV0Fl3zR0J4","{\"num\":\"035\",\"digital_key\":\"https://docs.google.com/document/d/15gXG9frqV0VrF57BjDqOe0pCHPWxVJ1ucV0Fl3zR0J4\",\"Title\":\"Build Control for Image Enhancement\",\"Author\":\"Evan Ruff\",\"Date\":\"05/11/18\"}"]}
May 11, 2018 4:59:41 PM com.google.api.client.http.HttpRequest execute
CONFIG: -------------- REQUEST  --------------
POST https://script.googleapis.com/v1/scripts/1T7HAaK2yJ2qUf3sOAz3ZGSPRql73-DGCe79-dLgCyqbnh_LbIn5KgQ4r:run
Accept-Encoding: gzip
Authorization: Bearer *******MY TOKEN *********
User-Agent: Memo Builder/1.0 Google-API-Java-Client Google-HTTP-Java-Client/1.23.0 (gzip)
Content-Type: application/json; charset=UTF-8
Content-Encoding: gzip
Content-Length: 230

May 11, 2018 4:59:41 PM com.google.api.client.http.HttpRequest execute
CONFIG: curl -v --compressed -X POST -H 'Accept-Encoding: gzip' -H 'Authorization: Bearer ya29.c.EmW4BdRkBR2JFBoDaAw_FG8DFbNCHYoe4E3jBs9HyowMAPqM2SnNky4ffRdh0zxG2nc4ylcIlr9yUHJ-ibOJuXdJhakgTmEyC7R4xn8cdKEif7mSeaeRGV9XwYI4W3AkoRAz-sCWmw' -H 'User-Agent: Micro C-Memo Builder/1.0 Google-API-Java-Client Google-HTTP-Java-Client/1.23.0 (gzip)' -H 'Content-Type: application/json; charset=UTF-8' -H 'Content-Encoding: gzip' -d '@-' -- 'https://script.googleapis.com/v1/scripts/1T7HAaK2yJ2qUf3sOAz3ZGSPRql73-DGCe79-dLgCyqbnh_LbIn5KgQ4r:run' << $$$
May 11, 2018 4:59:41 PM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: Total: 299 bytes
May 11, 2018 4:59:41 PM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: {"function":"main2","parameters":["15gXG9frqV0VrF57BjDqOe0pCHPWxVJ1ucV0Fl3zR0J4","{\"num\":\"035\",\"digital_key\":\"https://docs.google.com/document/d/15gXG9frqV0VrF57BjDqOe0pCHPWxVJ1ucV0Fl3zR0J4\",\"Title\":\"Build Control for Image Enhancement\",\"Author\":\"Evan Ruff\",\"Date\":\"05/11/18\"}"]}
May 11, 2018 4:59:41 PM com.google.api.client.http.HttpResponse <init>
CONFIG: -------------- RESPONSE --------------
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
X-Frame-Options: SAMEORIGIN
Alt-Svc: hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35"
Cache-Control: private
Server: ESF
X-Content-Type-Options: nosniff
Content-Encoding: gzip
Vary: Referer
Vary: X-Origin
Vary: Origin
X-XSS-Protection: 1; mode=block
Date: Fri, 11 May 2018 20:59:41 GMT
Content-Type: application/json; charset=UTF-8

This seems simple enough but I'm really struggling. Any tips would be appreciated!


Solution

  • In case someone comes across this, Google released a Java Docs API soon after I actually got this working. The Docs API is a much nicer solution.