How to use Resmush.it API from Apps Script (multi files upload) - google-apps-script

I'm trying to use the resmush.it API from Apps Script and I'm struggling when making the request with UrlFetch.
From their documentation, I understand they need a files array, in a multipart/form-data request.
What I'm doing so far (the images come from a Google Slides)
function myFunction() {
const OPTIMIZER_URL = "http://api.resmush.it/ws.php"
let slides = SlidesApp.openById("1TUZSgG_XXX_ni6VhdbE5usRyMc");
let pages = slides.getSlides();
pages.forEach((page) => {
let images = page.getImages();
images.forEach(image => {
let payload = {
files: [{
file: image.getBlob()
}]
}
let options = {
method: "POST",
payload: payload,
muteHttpExceptions : true
}
let response = UrlFetchApp.fetch(OPTIMIZER_URL, options)
let jsonResponse = JSON.parse(response.getContentText())
console.log(jsonResponse);
})
})
}
I also tried with payload = { file: image.getBlob() } but no success.
I get this error everytime:
{ error: 400,
error_long: 'No file or url provided',
generator: 'reSmush.it rev.3.0.4.20210124' }
Can you see what is wrong ?
Thank you

Although I'm not sure whether from your provided document I could correctly understand the specification of the API you want to use, how about the following modified script?
When I saw your script, I'm worried that your image.getBlob() has no name. In that case, the data is not sent as files. I thought that this might be the reason for your issue. When my this guess is reflected in your script, it becomes as follows.
Modified script:
function myFunction() {
const OPTIMIZER_URL = "http://api.resmush.it/ws.php"
let slides = SlidesApp.openById("1TUZSgG_XXX_ni6VhdbE5usRyMc");
let pages = slides.getSlides();
pages.forEach((page) => {
let images = page.getImages();
images.forEach(image => {
let options = {
method: "POST",
payload: { files: image.getBlob().setName("sample") },
muteHttpExceptions: true
}
let response = UrlFetchApp.fetch(OPTIMIZER_URL, options)
console.log(response.getContentText());
})
})
}
In this modification, the request body uses { files: image.getBlob().setName("sample") }. I'm not sure about the detailed specification of the API you want to use. So, if the key of files cannot be used, please modify files to file and test it again.
And, if your API is required to use the unique names of the uploaded files for each request, please set the unique names by modifying sample of image.getBlob().setName("sample").
Reference:
fetch(url, params)

Related

Google Apps Script - URL Fetch App returning random numbers

I am new to Apps Script and was trying to build an API and call that API through a different script. I created the web app and published it.
This is the URL:
https://script.google.com/macros/s/AKfycbxKVmGy3fxDfoHxyDtQh7psqj7IdKF7qHbgxLAwNRoiKTA-bpKN4QKtArzwsYdFb-Hb/exec
When I open this link, I can see the data correctly but when I try to fetch this data from a different script using urlfetchapp, it returns random numbers. I need help on what I am doing incorrectly.
Script which I am using to call this data:
function GetCopies()
{
var options = {
'contentType': "application/json",
'method' : 'get',
};
var Data = UrlFetchApp.fetch('https://script.google.com/macros/s/AKfycbxKVmGy3fxDfoHxyDtQh7psqj7IdKF7qHbgxLAwNRoiKTA-bpKN4QKtArzwsYdFb-Hb/exec',options)
Logger.log(Data.getContent())
}
This is the log I get:
I tried parsing it, but it throws an error:
How can I get data from URL correctly?
A working sample:
Create two Google Apps Script projects. In my case API and fetcher
API
const doGet = () => {
const myObj = {
"name": "Mr.GAS",
"email": "mrgas#blabla.com"
}
return ContentService
.createTextOutput(JSON.stringify(myObj))
.setMimeType(
ContentService.MimeType.JSON
)
}
fetcher
const LINK = "API_LINK"
const fetchTheAPI = async () => {
const options = {
'contentType': "application/json",
'method': 'get',
}
const res = UrlFetchApp.fetch(LINK, options)
const text = res.getContentText()
console.log(JSON.parse(text))
}
Deploy the API: Select type > Web app and Who has access > Anyone, copy the URL (it is important to copy that URL not the one redirected in the browser)
Replace the "API_LINK" by the URL.
Run the function.
You only need to adapt this example to suit your needs.
Documentation:
Content Service
Web Apps

Convert image/jpeg file sent as image/jpeg via REST API to AWS Lambda into a usable format

CURRENTLY
I am trying to send an image file to Lambda via API, such that it can be onsent to Textract.
Textract requires the file to be converted to "bytes", which I successfully achieved with this code: https://stackoverflow.com/a/69868101/1730260 but this code sent the file as a string, which only works for small files. Larger files converted to strings get too big i.e. bigger than 10MB, and then API Gateway will not accept.
So, instead, I am trying to send the image file in blob format.
I have a REST API request from Google Apps Script that looks like this:
Google Apps Script js:
let folder = DriveApp.getFolderById(folder_id);
let files = folder.getFiles();
let file = files.next();
let package = file.getBlob();
var options = {
method: "POST",
headers: {
"Content-Type": "image/jpeg",
},
payload: package,
};
var response = UrlFetchApp.fetch("https://api_address",options);
API Gateway Configurations
Type: LAMBDA_PROXY
Content Encoding: false
Binary Media Types: image/png image/jpeg
AWS Lambda js:
"use strict";
const AWS = require("aws-sdk");
exports.handler = async (event) => {
let file = event.body;
console.log("file:"+file)
let textract = new AWS.Textract();
let doc = params["doc"];
let config = {
Document: {
Bytes: Buffer.from(file), //<--- this is where issue is happening
FeatureTypes: ["TABLES"],
}
};
textract.analyzeDocument(config, function (err, data) {
console.log("analyzing...");
if (err) {
console.log(err, err.stack);
}
// an error occurred
else {
console.log("data:" + JSON.stringfy(data));
} // successful response
});
}
Console Log Output:
file:/9j/4AAQSkZJRgABAQAASABIAAD/4RvaRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAYAAAEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAAZgAAAMAAAABIAAAAAQAAAEgAAAABAAeQAAAHAAAABDAyMjGRAQAHAAAABAECAwCgAAAHAAAABDAxMDCgAQADAAAAAf//AACgAgAEAAAAAQAAD8CgAwAEAAAAAQAAC9CkBgADAAAAAQAAAAAAAAAAAAYBAwADAAAAAQAGAAABGgAFAAAAAQAAAQ4BGwAFAAAAAQAAARYBKAADAAAAAQACAAACAQAEAAAAAQAAAR4CAgAEAAAAAQAAGrIAAAAAAAAASAAAAAEAAABIAAAAAf/Y/9sAhAABAQEBAQECAQECAwICAgMEAwMDAwQFBAQEBAQFBgUFBQUFBQYGBgYGBgYGBwcHBwcHCAgICAgJCQkJCQkJCQkJAQEBAQICAgQCAgQJBgUGCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQn/3QAEAAr/wAARCAB4AKADASIAAhEBAxEB/8QBogAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoLEAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGR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1soiW5wVs4jwfTn0FdzLNyQeRXDeC5g0d8gmyitbBFYFFVRaQjEYP8P4DntxXXFI5E3ow/A1c9zekPk1MWyHHPpWGNTka63NgA9qkeJXzudfoKhW3gU43Dikbm+ssc68qPSuB0/xlp2oWN1qk0f2eK2uJYOWB3eUcbuAMfSulWWON9iEHd79Pwr4V+IXjG58O/DsNE23zJJ5frvkNVGN0Zznyn//T+4ZdhPC8mgRqsWMYq6ZF6oBxVKWQMdq1+SRfQ/SYuxn3C45WsxmGMmtKYAjn8qzZBg5xWiKkjntRtTK2fyqtEZIVwDjHpWpNhs9vQVQeJgMngU5EtWKo1Sa0vMRrtXaNxHfk12FhqqXYxnLY71wOoTxI6Rjjisjwm1/deMNSgnlxDELbyFPHLI5YL6/dzgVKRDdj2JsH5m4zx+VfMnx5hjl8QeGN5Chb2PsT/BMMDHTPv/hX09NGu0etfPXxb0q31DWNLkuGcGyYToFIALjco3ccjDHjjtV09xVNjtvtC6g91BOkeLS4MK7c52+WjDdn+L5u3GMVJ9liOhRhsEe31quw0u3lu2sJTI8l0WnGfuyeWg2jgcBQuKjkldNFQxHI6fTDUWHY6rwFYWcOqR3kMStILpACeowEr6o+JCwnwvqckh8sLaTneASVwh5wOTj2r478C+Jbez1+CzuuN93Gox6tsAr7N8ftDF4U1WW8G6NLSbcAM8bDnjI7e4rnqrVETPD/AAtoqRnVWniTabtdm0g5At4QTxnHOeDg+1bc2lW4BMOUPpU/hiOOP+1ect9uOeMDiGEDAyeMYx0+lXJXKls96fPqaUldHKy6esYO7is24tXH3elbtxnnzQNtU5p8Q7UGM1odFjlTazxzBl5Gcmvze+MR15fh/ef8JNa/ZbmK9vkVFj8pTEJ3MTKuBwyEHPfrX6feeip5khG0V8I/taPE+izMnQp0FaQfQyrxvE//1Ps2RpoOM59qrx3Fz6cCujuQjSciue8QXb6RpNxqNrD9okgQssQ4LkdAO....
UnsupportedDocumentException: Request has unsupported document format
NOTES
if I stringify the payload in the request, the size gets above 10MB and returns error HTTP content length exceeded 10485760 bytes
REQUEST
This is the format I need to get my code working (as it worked before with a different smaller image):
-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,-1,-31,0,34,69,120,105,102,0,0,77,77,0,42,0,0,0,8,0,1,1,18,0,3,0,0,0,1,0,1,0,0,0,0,0,0,-1,-37,0,67,0,2,1,1,2,1,1,2,2,2,2,2,2,2,2,3,5,3,3,3,3,3,6,4,4,3,5,7,6,7,7,7,6,7,7,8,9,11,9,8,8,10,8,7,7,10,13,10,10,11,12,12,12,12,7,9,14,15,13,12,14,11,12,12,12,-1,-37,0,67,1,2,2,2,3,3,3,6,3,3,6,12,8,7,8,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,-1,-64,0,17,8,0,-96,1,86,3,1,34,0,2,17,1,3,17,1,-1,-60,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-60,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-3,-4,-94,-118,40,0,-94,-118,51,64,5,20,102,-118,0,40,-93,52,102,-128,10,40,-93,56,-96,2,-118,51,70,104,0,-94,-116,-47,64,5,20,81,64,5,20,102,-118,0,40,-93,52,80,1,69,20,80,1,69,25,-93,52,0,81,70,115,70,104,0,-94,-118,40,0,-94,-116,-47,-102,0,40,-94,-118,0,40,-93,52,102,-128,10,40,-51,20,0,81,69,25,-51,0,20,81,69,0,20,81,69,0,20,81,69,0,20,81,65,25,20,1,-7,47,-1,0,7,25,-1,0,-63,121,-11,111,-8,39,-57,-123,116,95,3,-4,18,-15,39,-128,117,47,-120,-98,32,-110,-10,-49,93,-100,106,16,106,58,-105,-126,-68,-91,-73,104,-53,-39,-85,31,46,105,68,-20,80,-36,46,-36,68,112,-115,-37,-33,-2,15,-2,-35,30,27,-8,-7,-1,0,4,-127,-43,-75,15,11,-4,92,-16,-25,-117,62,38,120,119,-32,-16,-44,-11,-7,52,95,18,-63,121,-84,-23,26,-127,-46,89,-102,107,-127,4,-122,88,38,-13,-43,-50,91,107,7,83,-36,87,-28,23,-4,29,-25,-1,0,4,-11,-16,111,-20,-51,-5,68,120,87,-30,-42,-123,-87,120,-102,-21,-60,-97,27,-81,-11,75,-67,118,-38,-2,-26,9,44,109,26,-46,59,4,-116,90,-94,66,-114,-128,-119,91,119,-104,-14,100,-127,-116,119,-5,-53,-2,9,-13,-1,0,4,-110,-8,111,-5,4,-1,0,-63,37,-66,38,124,81,-16,126,-75,-29,125,75,-60,31,25,-66,10,-117,-19,110,-33,89,-68,-74,-102,-50,-43,-37,74,-106,-28,-117,101,-118,-34,39,85,-33,51,-113,-99,-36,-32,47,57,-55,60,77,-33,47,-83,82,91,-21,126,-23,-88,-67,23,-111,-76,-76,-59,80,75,-82,-53,-70,-26,87,111,-51,108,121,7,-4,26,45,-5,108,-8,-5,-29,38,-105,-5,69,106,-1,0,25,62,46,120,-61,-59,90,71,-123,32,-48,110,34,-68,-15,-97,-118,110,47,-83,-76,120,-33,-5,75,-51,117,-110,-22,86,88,85,-74,38,-30,8,7,98,-25,-96,-81,-39,47,-126,63,-75,47,-61,63,-38,94,-38,-10,-29,-31,-57,-60,111,2,-4,65,-73,-45,89,82,-18,79,13,-21,-10,-102,-78,90,-79,-28,9,12,18,56,66,123,3,-118,-2,90,-1,0,-32,-34,95,-8,37,-60,-65,-16,85,-1,0,27,124,74,-16,63,-119,60,127,-30,-97,7,-4,39,-47,98,-45,-75,79,18,105,-38,4,-47,-59,117,-81,93,-122,-71,91,21,-52,-120,-15,-127,22,110,95,115,-58,-8,36,0,-71,109,-53,-42,-4,22,-8,53,117,-1,0,4,93,-1,0,-125,-103,60,39,-16,-49,-64,-34,46,-42,47,52,24,124,87,-91,120,125,-18,46,72,73,-75,29,55,86,-118,-40,-101,107,-91,-116,4,-112,-81,-38,83,-99,-95,119,-60,-110,5,66,6,-33,102,-93,-115,74,-16,-92,-43,-100,-94,-83,-14,73,107,-22,-1,0,-49,-56,-29,-42,49,-85,82,58,-14,-69,-65,77,54,-4,62,-13,-12,-61,-2,14,14,-8,89,-15,-125,-57,-97,-76,-33,-127,-18,62,29,-2,-38,31,13,-1,0,102,-99,46,15,15,121,119,90,23,-120,-2,46,94,-8,54,-29,86,-105,-19,50,-97,-76,-91,-68,10,86,101,-38,66,121,-121,-100,-95,94,-43,-6,-65,117,-81,89,-8,79,-62,-115,-88,106,-102,-123,-99,-91,-115,-115,-65,-99,115,123,115,58,-57,12,72,-85,-106,-111,-92,98,0,80,50,75,19,-46,-65,-100,79,-8,61,27,63,-16,-33,31,9,73,-1,0,-95,47,-113,-4,14,-72,-81,90,-1,0,-125,-51,60,115,-15,19,72,-16,-41,-64,-51,5,103,-43,97,-8,67,-86,-59,115,62,-93,21,-95,49,-61,123,-87,-60,-47,20,73,-37,5,75,44,37,-102,37,110,50,101,108,29,-68,112,81,111,-22,112,107,-19,84,-102,-41,-90,-69,-73,-67,-69,47,67,-94,-92,111,93,47,-18,39,-89,-53,75,126,-89,-20,111,-122,63,-32,-92,95,-77,-65,-115,60,81,30,-121,-93,124,122,-8,47,-85,-21,83,73,-28,-57,97,101,-29,109,50,123,-87,31,56,-38,34,73,-117,19,-98,49,-116,-26,-67,-81,-17,-118,-2,109,-4,47,-5,59,-1,0,-63,37,-1,0,110,-81,-128,-70,95,-123,-68,15,-29,-83,87,-10,117,-8,-101,42,64,127,-74,60,93,119,-88,25,81,-112,15,53,110,77,-52,-25,76,111,49,67,-1,0,-86,-106,44,57,82,-72,31,35,126,-23,127,-63,58,127,101,-85,31,-40,-33,-10,71,-16,-97,-128,116,111,-120,-98,38,-8,-95,-96,-23,118,-34,102,-107,-83,-21,87,80,93,72,-10,-78,124,-15,-57,12,-112,-94,-125,108,-96,-2,-20,51,57,85,96,-95,-54,4,11,-73,37,-94,-17,-70,-47,118,126,-116,-61,-38,93,-85,117,-35,117,94,-89,-69,63,8,107,-14,7,-32,31,-62,-49,-116,86,31,-16,95,75,-3,123,80,-3,-75,62,27,-8,-117,-31,-21,120,-105,90,-111,62,15,-37,-4,93,-66,-68,-42,-96,-123,-84,-18,-124,86,-89,67,101,-14,85,-32,98,-110,52,121,-60,98,34,-61,-107,21,-6,-5,39,-6,-93,-12,-81,-26,-109,-10,55,-1,0,-107,-56,-75,-81,79,-8,78,-4,83,-49,-3,-62,-11,10,-116,42,-26,-58,-58,31,-35,-109,-5,-83,-45,-11,53,-83,-90,26,114,-13,95,-87,-3,24,124,88,-8,-35,-32,-33,-128,126,23,58,-25,-114,-68,91,-31,-113,6,104,-95,-4,-77,-88,107,-70,-84,26,117,-82,-20,19,-73,-51,-103,-107,115,-128,78,51,-40,-41,55,-16,115,-10,-42,-8,57,-5,67,-21,114,105,-65,15,-66,45,124,52,-15,-34,-95,18,-105,123,95,15,120,-98,-53,84,-103,64,25,36,-92,18,-69,0,7,-75,127,55,-65,-16,88,-17,-118,-102,76,-33,-16,113,6,-83,15,-19,117,97,-29,111,16,124,24,-16,-19,-36,80,-40,-24,-6,68,-51,9,26,59,-38,-122,-73,107,113,-67,9,-119,-25,59,-90,49,58,-69,17,48,86,-36,-95,107,-24,-49,-121,127,-16,76,-1,0,-8,38,-17,-19,-9,-15,-37,-64,122,-73,-20,-43,-5,67,95,124,19,-42,-76,-55,-48,73,-31,-21,93,78,-14,-41,90,-44,-18,-53,6,-127,-84,95,86,-109,-49,75,-108,96,-64,-104,60,-27,63,41,10,-72,-36,-58,30,-11,35,25,-67,-91,-37,116,-68,-41,127,33,86,-3,-37,113,-66,-85,-85,-39,-7,35,-6,6,92,-48,79,56,-23,89,-2,28,-45,38,-48,-4,59,99,99,54,-95,121,-85,77,103,111,28,18,95,93,-120,-59,-59,-37,42,-123,50,-55,-27,-94,71,-67,-120,-36,118,34,-82,73,-62,-127,-127,89,127,22,116,29,115,-59,-97,11,-68,67,-91,-8,103,92,-113,-61,30,35,-44,116,-21,-117,109,51,87,-110,-48,94,46,-105,114,-15,-78,-59,112,97,44,-94,79,45,-56,109,-123,-128,59,113,-102,-102,-115,-59,54,-75,-73,-30,58,122,-38,-3,78,71,-29,47,-19,-73,-16,103,-10,116,-41,-93,-46,126,33,124,91,-8,103,-32,93,86,104,-60,-47,-39,-8,-121,-59,22,58,93,-60,-120,122,48,-114,121,81,-120,-29,-82,49,93,87,-62,-17,-116,-34,17,-8,-29,-31,72,-11,-17,4,-8,-85,-61,-66,49,-48,-26,109,-111,-22,58,30,-91,14,-95,105,35,0,9,2,88,89,-112,-112,8,-17,-36,87,-32,42,127,-63,-75,-33,2,126,2,-8,-69,-57,26,-41,-19,-91,-5,93,-23,54,122,-10,-85,124,111,-76,-87,52,-49,19,89,-23,122,-90,-92,-81,-103,38,-70,-68,93,70,41,-26,-106,121,29,-119,-39,18,-74,54,-106,50,57,124,39,-127,-1,0,-63,-74,127,19,-96,-3,-97,127,-32,-68,-119,-32,63,-121,-98,49,-44,60,65,-16,-33,-59,-105,58,-26,-122,-105,107,27,67,23,-120,-84,109,-83,-82,-82,44,-18,100,-123,-62,-107,111,-36,35,-126,84,50,-17,97,-128,25,-123,107,-121,-118,-87,63,103,125,108,-35,-42,-38,43,-40,49,30,-28,28,-29,-78,105,107,-66,-67,79,-22,112,114,42,-98,-67,-81,-39,120,91,70,-70,-44,-75,75,-53,93,55,79,-79,-119,-89,-71,-70,-70,-103,97,-122,-34,53,25,103,119,98,21,84,14,73,39,2,-82,-109,-111,95,-126,-97,-16,122,-81,-114,-2,35,104,-10,-33,6,116,17,54,-83,15,-62,29,85,-81,39,-44,34,-76,38,56,47,117,56,-98,50,-119,59,96,-87,101,-124,-77,68,-83,-58,76,-83,-125,-73,-114,122,-110,105,-58,11,121,59,93,-20,-68,-39,116,-29,-51,123,-10,-71,-5,3,-31,-97,-8,41,23,-20,-17,-29,79,20,71,-94,104,-65,30,-66,11,-22,-38,-44,-46,121,49,-40,89,120,-33,76,-72,-70,119,-50,54,-120,-110,98,-60,-25,-116,99,57,-81,89,-15,71,-118,116,-65,2,-8,103,80,-42,-75,-115,66,-57,72,-47,-76,-101,105,47,111,-81,-81,110,18,-34,-38,-54,8,-44,-68,-110,-53,35,-112,-88,-118,-96,-77,51,16,0,4,-109,95,-50,79,-122,63,103,127,-8,36,-73,-19,-43,-16,19,74,-16,-65,-127,-4,117,-86,-2,-50,-65,19,38,72,15,-10,-49,-117,-81,53,3,42,50,1,-26,-83,-55,-71,-100,-23,109,-26,0,-1,0,-22,-91,-117,14,84,-82,7,-56,-33,-85,-66,49,-3,-105,-84,127,99,-113,-8,32,-17,-59,79,1,104,-1,0,17,60,75,-15,59,65,-45,62,25,107,-14,105,90,-34,-75,117,5,-52,-113,107,38,-99,59,-57,28,50,66,-118,13,-80,7,-9,97,-103,-54,-85,5,14,80,32,91,-81,-5,-70,51,-101,-34,59,118,122,116,100,97,-1,0,121,86,20,-42,-46,122,-9,71,-45,62,12,-3,-74,126,13,124,71,-16,22,-79,-30,-81,14,-4,91,-8,103,-81,-8,95,-61,-82,-79,-22,-70,-66,-101,-30,-117,27,-85,13,49,-40,101,86,121,-29,-108,-57,17,35,-96,118,25,-83,-97,-126,-1,0,-76,-89,-61,-81,-38,75,73,-70,-44,62,30,120,-13,-63,-98,61,-80,-79,113,21,-59,-49,-121,53,-69,109,86,24,28,-25,10,-17,3,-72,83,-63,-32,-98,-43,-4,-73,127,-63,-67,127,-16,72,104,-1,0,-32,-81,30,34,-15,-18,-125,-29,111,28,120,-53,-61,127,9,-4,10,109,117,11,-51,55,67,-70,88,-28,-44,53,43,-107,-98,56,29,4,-55,36,8,-55,28,50,-18,-112,-60,-49,-126,-86,48,27,35,-109,-15,55,-20,-83,-15,27,-2,9,-7,-1,0,5,-84,-15,39,-20,-33,-16,63,-30,87,-119,60,51,117,-30,-83,94,-33,-64,-42,-6,-22,-56,18,-22,77,47,85,-114,6,-1,0,72,-14,-128,5,-93,-114,117,37,-93,8,-37,-94,-34,-98,91,109,-58,-114,-101,85,85,30,-78,87,75,-18,-33,-17,-5,-75,38,82,92,-110,-102,122,65,-21,-23,-1,0,13,-8,-24,127,84,-6,119,-19,-89,-16,119,87,-8,-78,-34,0,-76,-8,-79,-16,-42,-21,-57,107,49,-73,111,13,-59,-30,123,39,-43,-42,65,-43,13,-88,-105,-50,-36,61,54,-26,-67,57,8,-57,-14,-81,-27,-85,-2,14,0,-1,0,-126,6,120,51,-2,9,23,-16,87,-31,-97,-114,-68,1,-29,111,25,107,-17,-81,106,-115,-93,-21,63,-37,-113,110,-52,47,4,6,104,-25,-74,48,-57,25,-119,15,-107,54,81,-116,-124,124,-97,63,28,-2,-42,126,-53,67,-30,-97,-4,20,35,-2,8,65,-32,56,116,95,-119,87,94,7,-8,-103,-29,-65,4,91,89,-65,-116,94,-35,-81,46,-94,-111,72,-118,105,-2,89,35,113,52,-79,-58,-29,-51,13,-71,26,77,-29,44,5,102,-41,-18,37,86,58,-72,-76,-97,77,89,95,-14,-10,49,122,41,43,-90,-9,-75,-19,-87,-12,87,-60,-65,-8,40,47,-64,95,-126,-98,48,-72,-16,-1,0,-116,-66,55,124,35,-16,-106,-65,104,64,-97,77,-42,-68,97,-89,88,94,67,-98,70,-24,-91,-103,92,117,-18,43,-46,60,29,-29,-67,23,-30,71,-123,-19,117,-65,14,-22,-6,94,-67,-92,95,-89,-103,107,127,-89,93,71,117,109,114,-67,50,-110,33,42,-61,32,-14,15,106,-2,117,127,-30,29,63,-39,67,-10,76,-8,59,-30,24,-1,0,106,47,-38,-41,67,-48,126,41,90,-51,119,37,-75,-121,-122,-4,65,98,-111,91,68,1,16,121,-106,51,-37,-55,125,115,33,32,59,-120,-62,99,126,-59,-55,30,99,65,-1,0,6,93,124,109,-41,116,-97,-37,39,-30,-121,-61,120,-75,-117,-55,-68,39,-86,120,81,-11,-1,0,-80,18,126,-51,-10,-69,123,-69,88,22,-31,84,-97,-111,-52,119,12,-89,3,-26,1,119,125,-59,-59,81,-116,106,115,65,61,82,-67,-42,-37,106,-67,73,-84,-35,52,-90,-74,-70,86,-21,-85,74,-25,111,-5,41,126,-38,63,24,-68,71,-1,0,7,92,-21,31,14,-11,15,-117,31,18,-81,-66,31,71,-29,63,18,90,-89,-123,-18,60,79,123,46,-116,-80,-59,-89,95,60,81,-117,54,-108...
How do I convert the string currently in my console log to the above format?
In case you are struggling with the API-gateway limit, then a better way would be to use Pre-signed URLs
You can upload upto 5GB with this method.
You can create a dedicated bucket for this purpose and configure a trigger.
As soon as a file is uploaded to bucket, lambda will be triggered and it can process your image.
You can upload the image in any appropriate format that you want.

Upload multiple attachments via Jira API using UrlFetchApp.fetch

I am using google app script server side coding to upload multiple files using Jira Rest API. I have used urlFetchApp.fetch() method for it. The payload i am passing is file blob data with 'Content-Type: false' as i am unable to pass it as formData through server side coding.
With this approach i am able to pass single file at a time to Jira attachments API, I have to call Jira API for each file.
Below is sample code,
function updateJiraTicket(fileUploadArr, ticketId){
const jira_Api = getJiraInstanceKeys();
const JIRA_URL= jira_Api["URL"] + ticketId + "/attachments";
const JIRA_LOGIN = jira_Api["LOGINNAME"];
const JIRA_PASSWORD = jira_Api["PASSWORD"];
const PROJECT = jira_Api["PROJECT"];
//fileUploadArr format getting passed from file upload UI side control
//fileUploadArr = [{“fileData”:”{base64 file data}”, “fileType”:”image/jpg”,”fileName”:”testimg1”},{“fileData”:”{base64 file data}”, “fileType”:”image/jpg”,”fileName”:”testimg2”}]
fileUploadArr.forEach(function(arr) {
let file = getFile(arr.fileData, arr.fileType, arr.fileName);
let formdata = {'file' : file };
let params = {
method: 'POST',
headers: {
'Authorization': 'Basic ' + Utilities.base64Encode(JIRA_LOGIN+':'+JIRA_PASSWORD),
'Content-Type': false,
'X-Atlassian-Token': 'no-check'
},
"payload": formdata,
muteHttpExceptions: true
}
let result = UrlFetchApp.fetch(JIRA_URL, params);
let response = result.getContentText();
})
}
function getFile(data, type, name) {
var file = Utilities.newBlob(Utilities.base64Decode(data), type, name);
return file;
}
Can anyone help with this?

How to change an Apify actor parameter via API

I want to call an Apify actor and specify a parameter value via a call to the Apify API.
The actor is the Google Search Results Scraper located here.
Here is where the docs say to use queries as the object property name in the API call payload.
The following table shows specification of the actor INPUT fields as defined by its input schema. These fields can be [...] provided in a JSON object when running the actor using the API. Read more in docs.
...
Search queries or URLs
Google Search queries (e.g. food in NYC) and/or full URLs (e.g. https://www.google.com/search?q=food+NYC).
Enter one item per line.
Optional
Type: String
JSON example
"queries": "Hotels in NYC
Restaurants in NYC
https://www.google.com/search?q=restaurants+in+NYC"
After I run my Google Apps Script code, I expect to see a change in the searchQueries.term parameter to be the following.
Apify — what I expect to see
"searchQuery": {
"term": "Banks in Phoenix", // what I am trying to change to by API call
// [...]
},
But what I actually get is the same parameter value as existed the last time I ran the actor manually. As follows.
Apify — what I actually see
"searchQuery": {
"term": "CPA firms in Newark", // remaining from last time I ran the actor manually
// [...]
},
Here is the code I'm running from Google Apps Script.
Code.gs
const runSearch = () => {
const apiEndpoint= `https://api.apify.com/v2/actor-tasks/<MY-TASK-NAME>/run-sync?token=<MY-TOKEN>`
const formData = {
method: 'post',
queries: 'Banks in Phoenix',
};
const options = {
body: formData,
headers: {
'Content-Type': 'application/json',
},
};
UrlFetchApp.fetch(apiEndpoint, options,);
}
What am I doing wrong?
You are missing the payload property in the request object.
Change:
queries: 'Banks in Phoenix',
to:
payload: {
queries: 'Banks in Phoenix',
}
Code.gs
const runSearch = () => {
const apiEndpoint= `https://api.apify.com/v2/actor-tasks/<MY-TASK-NAME>/run-sync?token=<MY-TOKEN>`
const formData = {
method: 'post',
payload: {
queries: 'Banks in Phoenix',
},
};
const options = {
body: formData,
headers: {
'Content-Type': 'application/json',
},
};
UrlFetchApp.fetch(apiEndpoint, options,);
}

How to add media upload for BigQuery Rest API using UrlFetchApp?

I need to stream data into BigQuery from my Google Apps Script addon.
But I need to use my service account only (I need to insert data into my BigQuery table, not user's BigQuery table)
I followed this example: https://developers.google.com/apps-script/advanced/bigquery#load_csv_data
Because Apps Script Advanced Service doesn't support service account natively, so I need to change this example a bit:
Instead of using Advanced Service BigQuery, I need to get the OAuth token from my service account, then using BigQuery Rest API to handle the same job:
This is what I did:
function getBigQueryService() {
return (
OAuth2.createService('BigQuery')
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Caching
.setCache(CacheService.getUserCache())
// Locking
.setLock(LockService.getUserLock())
// Set the scopes.
.setScope('https://www.googleapis.com/auth/bigquery')
)
}
export const insertLog = (userId, type) => {
const bigQueryService = getBigQueryService()
if (!bigQueryService.hasAccess()) {
console.error(bigQueryService.getLastError())
return
}
const projectId = bigqueryCredentials.project_id
const datasetId = 'usage'
const tableId = 'logs'
const row = {
timestamp: new Date().toISOString(),
userId,
type,
}
const data = Utilities.newBlob(convertToNDJson(row), 'application/octet-stream')
// Create the data upload job.
const job = {
configuration: {
load: {
destinationTable: {
projectId,
datasetId,
tableId,
},
sourceFormat: 'NEWLINE_DELIMITED_JSON',
},
},
}
const url = `https://bigquery.googleapis.com/upload/bigquery/v2/projects/${projectId}/jobs`
const headers = {
Authorization: `Bearer ${bigQueryService.getAccessToken()}`,
'Content-Type': 'application/json',
}
const options = {
method: 'post',
headers,
payload: JSON.stringify(job),
}
try {
const response = UrlFetchApp.fetch(url, options)
const result = JSON.parse(response.getContentText())
console.log(JSON.stringify(result, null, 2))
} catch (err) {
console.error(err)
}
}
As you can see in my code, I get the Blob data (which is the actual json data that I need to put in BigQuery table) using this line:
const data = Utilities.newBlob(convertToNDJson(row), 'application/octet-stream')
But I don't know where to use this data with the BigQuery Rest API
The documentation doesn't mention it: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert
How this can be done? Thank you.
I can solve this problem using Tanaike's FetchApp library:
https://github.com/tanaikech/FetchApp#fetch
Anyone has this issue in the future: please check my comment in code to understand what was done.
Turn out, the job variable is treated as metadata, and the data variable is treated as file in the form data object
// First you need to convert the JSON to Newline Delimited JSON,
// then turn the whole thing to Blob using Utilities.newBlob
const data = Utilities.newBlob(convertToNDJson(row), 'application/octet-stream')
// Create the data upload job.
const job = {
configuration: {
load: {
destinationTable: {
projectId,
datasetId,
tableId,
},
sourceFormat: 'NEWLINE_DELIMITED_JSON',
},
},
}
const url = `https://bigquery.googleapis.com/upload/bigquery/v2/projects/${projectId}/jobs?uploadType=multipart`
const headers = {
Authorization: `Bearer ${bigQueryService.getAccessToken()}`,
}
const form = FetchApp.createFormData() // Create form data
form.append('metadata', Utilities.newBlob(JSON.stringify(job), 'application/json'))
form.append('file', data)
const options = {
method: 'post',
headers,
muteHttpExceptions: true,
body: form,
}
try {
FetchApp.fetch(url, options)
} catch (err) {
console.error(err)
}
Note: When you create the service account, choose role BigQuery Admin, or any role that has permission bigquery.jobs.create
https://cloud.google.com/bigquery/docs/access-control#bigquery-roles
Because if you don't, you will have the error
User does not have bigquery.jobs.create permission...