0

I want to send a PDF document in the JSON request to Document AI Solutions, using Google Apps Script.

I'm using the OCR processor, here is the doc: https://cloud.google.com/document-ai/docs/ocr I set some variables from the GCP, such as PROJECT_ID, CLIENT_ID, CLIEND_PWD.

In addition, fileID is a file ID from my Drive. It's a PDF file.

Here is the code I used:

function test(){
    
  var result=getFileInformations(fileID);
  Logger.log(result);
  
}//function test(){

function getFileInformations (fileID) {
  
  //var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID:method
  var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID  + ":process"
  var token = ScriptApp.getOAuthToken();
  
  // Create our json request
  var nlData = {    
    document: {
      //'mimetype':MimeType.PDF,
      content:  DriveApp.getFileById(fileID).getBlob()
    }
  };//var nlData = {
      
  var options = {};
  options.headers = {"Authorization": "Basic " + Utilities.base64Encode(CLIENT_ID + ":" + CLIEND_PWD)};
  options.payload = JSON.stringify(nlData);
  
  //  And make the call
  var response = UrlFetchApp.fetch(apiEndpoint, options);
  var result = JSON.parse(response);
    
};//function getFileInformations (fileID) {

I'm getting an error message, which sounds like "Exception: Error came on the server. Please apologize and try later" and this points out to the line:

 content:  DriveApp.getFileById(fileID).getBlob()

Following the last information I got, here is my current situation. I think my first mistake was to separate my authentication method from my getFileInformations function, so I changed it to eventually get this:

The new main function is docAISolutionAPI. I don't think useful to display the getService() function there. Keep in mind that it works and the service.hasAccess() runs successful.

function docAISolutionAPI() {
  var service = getService();
  if (service.hasAccess()) {

    var response = getFileInformations (fileID)    
    Logger.log(response);    
   
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s',
        authorizationUrl);
  }
}//function docAISolutionAPI() {


function getFileInformations (fileID) {
  
  //var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID:method
  var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID  + ":process";
    
 var token = ScriptApp.getOAuthToken();

  var test=DriveApp.getFileById(fileID).getName();

  // Create our json request
  var param = {    
    'document': {
    'mimeType':'application/pdf',
    'content':  DriveApp.getFileById(fileID).getBlob()
    // 'content':jsonContent
  }//inputConfig': {
  };//var param = {
  
  var jsonresquest=JSON.stringify(param);
  var options = {};
  options.headers = {Authorization:"Bearer "+token};
 // options.payload = JSON.stringify(param);
  options.payload = param;
  options.method = "POST";
  options.muteHttpExceptions=true;

  Logger.log("options.payload : " + options.payload);
  
  //  And make the call
  var response = UrlFetchApp.fetch(apiEndpoint, options);
  var result = JSON.parse(response);
    
    return result;
};//function getFileInformations (fileID) {

As you can see, there are some comments on failed attempts to get something.

The error I get now is:

{error={details=[{fieldViolations=[{description=Invalid JSON payload received. Unknown name "document": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types.}], @type=type.googleapis.com/google.rpc.BadRequest}], status=INVALID_ARGUMENT, message=Invalid JSON payload received. Unknown name "document": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types., code=400.0}}
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • Your error message `Exception: Error came on the server. Please apologize and try later` doesn't seem to return any relevant results, was this the original message or has this been translated? Can you post the original error if so? – Rafa Guillermo Dec 16 '20 at 08:37
  • Hi, this is a French message, so I took the liberty to translate it. – Aurélien Moyen Dec 17 '20 at 09:50
  • Check out [this meta post](https://stackoverflow.com/questions/2506959/is-it-right-to-translate-error-messages) regarding error translation. Please can you post the original error? – Rafa Guillermo Dec 17 '20 at 10:03
  • Did you take that authentication method from an official sample? I'm asking because the documentation mentions service account only: https://cloud.google.com/document-ai/docs/before-you-begin?hl=en_US – mshcruz Dec 17 '20 at 10:04
  • @RafaGuillermo I've been working on my code since, and I have not the same error. Now it's an error on the urlFetchApp method. – Aurélien Moyen Dec 18 '20 at 12:35
  • @mshcruz it's a mix of various methods I found, picking some informations on the doc you mention and some methods found on the stackoverflow website and other sites. I'm afraid now my issue is different. I will edit the new complete code with error found. – Aurélien Moyen Dec 18 '20 at 12:37
  • @RafaGuillermo, I just posted an update on my situation, dealing with another point but the issue is the same in the end :p – Aurélien Moyen Dec 18 '20 at 12:53

1 Answers1

0

It looks like your input to the API is not formatted correctly. Refer to this documentation for how to send a processing request with the v1 endpoint.

https://cloud.google.com/document-ai/docs/send-request

Your request is structured like this

'document': {
    'mimeType':'application/pdf',
    'content':  DriveApp.getFileById(fileID).getBlob()
    // 'content':jsonContent
  }//inputConfig': {

Where it should be in this structure. Using rawDocument instead of document. Some docs may have been out of date when this question was posted.

'rawDocument': {
    'mimeType':'application/pdf',
    'content':  DriveApp.getFileById(fileID).getBlob()
    // 'content':jsonContent
  }//inputConfig': {
double-beep
  • 5,031
  • 17
  • 33
  • 41
Holt Skinner
  • 1,692
  • 1
  • 8
  • 21