function processDocument()

in apps-script-google-drive/documentai.gs [42:89]


function processDocument(file) {
  var service = getService();
  if (!service.hasAccess()) {
    Logger.log(service.getLastError());
    return;
  }

  // Processor name, should look like: `projects/${projectId}/locations/${location}/processors/${processorId}`;
  const name =
    "projects/" +
    PROJECT_ID +
    "/locations/" +
    LOCATION +
    "/processors/" +
    PROCESSOR_ID;
  var apiEndPoint =
    "https://" +
    LOCATION +
    "-documentai.googleapis.com/v1/" +
    name +
    ":process";

  var docBytes = Utilities.base64Encode(file.getBlob().getBytes());
  var mimeType = file.getMimeType();

  // Creates a structure with the text and request type.
  var requestData = {
    name,
    rawDocument: {
      content: docBytes,
      mimeType: mimeType,
    },
  };

  // Packages all of the options and the data together for the call.
  var options = {
    method: "post",
    contentType: "application/json",
    headers: {
      Authorization: "Bearer " + service.getAccessToken(),
    },
    payload: JSON.stringify(requestData),
  };
  //  And makes the call.
  var response = UrlFetchApp.fetch(apiEndPoint, options);
  var data = JSON.parse(response);
  return data;
}