Calling Vantage API from Google AppScript. Launch transaction

Google AppScript is an extension language to create add-ons for Google products such as Gmail or GDrive. For example you can create a script to read data from attached invoices.

AppScript help center has examples how to call external services. The main method is Class UrlFetchApp  |  Apps Script  |  Google Developers

For example here is the function to authenticate on Vantage server

// define global constants
const VANTAGE_URL= "https://server.vantage.abbyy.com"

function VantageAuthenticate(login,password) {

// define headers
var headers = {
'Content-Type' : 'application/x-www-form-urlencoded'
}

// define request body
var payload ={
'grant_type' : 'password',
'scope':'openid permissions',
'username':login,
'password':password,
'client_id':'ABBYY.Vantage',
'client_secret':'f3ec6136-6ccc-75a1-3780-caeef723e998'
}

// set up REST call parameters
var options = {
'headers': headers, // headers
'method' : 'post', // call type
'payload' : payload // call body
};

// call rest api
var response=UrlFetchApp.fetch(VANTAGE_URL+'/auth/connect/token', options);

// read response
var json = response.getContentText();

// parse json response to objects
var data = JSON.parse(json);

// read access token
var token=data.access_token;

return token;
}

 

It can be tricky to start a transaction because you have to upload files with multipart POST call. For uploading files use new blob where put file bytes, name and content type. The example is below.

 

// define global constants
const VANTAGE_URL= "https://server.vantage.abbyy.com"

// create and run transaction.
function LaunchTransaction(accesstoken,skillid, filebytes, fileName) {
/*
accesstoken - authentication token
skillid - skill identifier
filebytes - file in bytes (not Base64 string)
fileName - the name of the file
*/

var url=VANTAGE_URL+'/api/publicapi/v1/transactions/launch?skillId='+skillid

var formdata = Utilities.newBlob(filebytes,'application/pdf', fileName) // the sample is for PDF files. 

// create form for formdata POST request
var form = {
'files' : formdata,
}

// create headers
var headers = {
'Authorization' : 'Bearer '+ accesstoken
}

// create options for POST call
var options = {
'headers': headers,
'method' : 'post',
'payload' : form
};

// make POST
var response=UrlFetchApp.fetch(url, options);

// process responce
var json = response.getContentText();
var data = JSON.parse(json);
var transactionID=data.transactionId;

return transactionID;
}

 

 

Was this article helpful?

1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.