I am new to SO.
I am trying to hit an api using put request to upload the images and I wanted to do it using google sheets and google apps script. I am able to add images from url to the sheet but not to the api because it accepts only .png/.jpeg format and I think currently it is going in blob format. Not sure.
Can anyone please help me on how to send image to api using google apps script and sheet.
I am able to get success response using postman when in params I type image as key (filetype) and then upload image from device.
Here is some code that I am using to achieve the above:
function insertImageOnSpreadsheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var response = UrlFetchApp.fetch('Enter URL Here');
var binaryData = response.getContent();
// Insert the image in cell A1.
var blob1 = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
Logger.log(blob1)
var apiHeaders =
{
"Content-Type":"application/json",
"Authorization":"Enter Token here"
}
var formData= '{"image": "'+blob1+'"}'
var eventApiResponse = callApi('Enter API Here',"put",formData,apiHeaders,"insertImageOnSpreadsheet");
Logger.log(eventApiResponse)
sheet.insertImage(blob1, 1, 3);
}
function callApi(targetURL, method, formData, headers,functionName) {
var options = {
"method": method,
"payload":formData,
"headers":headers}
var response = UrlFetchApp.fetch(targetURL, options);
return response;
}
API : livestream.com/developers/docs/api/#update-event-logo-poster
Somebody pleaasseee help
I believe your goal as follows.
When I saw the sample curl command of "Update Event Logo (Poster)", it is as follows.
curl -X PUT \\ -u [API_SECRET_KEY] \\ -F logo='@<image file location>;type=<image type>' \\ --header 'Content-Type: multipart/form-data' \\ 'https://livestreamapis.com/v3/accounts/18855759/events/5201483/logo'
{"logo": "data"}
to "files" using multipart/form-data
.multipart/form-data
, it is required to create the request body.
In your script,
"Authorization":"Enter Token here"
. But from the sample curl command, I thought that "API_SECRET_KEY" was not encoded with the base64."Content-Type":"application/json"
is not used.When your script is modified it becomes as follows.
const url = "###"; // Please set URL you want to use. It's like "https://livestreamapis.com/v3/accounts/18855759/events/5201483/logo".
const API_SECRET_KEY = "###"; // Please set your API_SECRET_KEY
// var binaryData = UrlFetchApp.fetch('Enter URL Here').getContent();
const binaryData = DriveApp.getFileById("### fileId ###").getBlob().getBytes(); // Modified
let data = "--xxxxxxxxxx\r\n";
data += "Content-Disposition: form-data; name=\"logo\"; filename=\"sample.png\"\r\n";
data += "Content-Type: image/png\r\n\r\n";
const payload = Utilities.newBlob(data).getBytes()
.concat(binaryData)
.concat(Utilities.newBlob("\r\n--xxxxxxxxxx--").getBytes());
const options = {
method : "put",
contentType : "multipart/form-data; boundary=xxxxxxxxxx",
payload : payload,
headers: {authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY)},
};
const res = UrlFetchApp.fetch(url, options);
console.log(res.getContentText())
var blob1 = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
sheet.insertImage(blob1, 1, 3);
UrlFetchApp.fetch('Enter URL Here');
returns the binary data of the image of PNG or JPEG. Please be careful this.data
is important. So please be careful this.authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY)
as the authorization header. But if API_SECRET_KEY
is the same with "Basic " + Utilities.base64Encode(API_SECRET_KEY)
, please try to modify to authorization : "API_SECRET_KEY"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.