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}}