[英]Read Images from system or url using google apps script in google sheets and post image to an API
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.我正在尝试使用 put 请求上传图像来访问 api,我想使用谷歌表格和谷歌应用程序脚本来完成。 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.
我可以将图像从 url 添加到工作表,但不能添加到 api,因为它只接受 .png/.jpeg 格式,我认为目前它采用 blob 格式。 Not sure.
没有把握。
Can anyone please help me on how to send image to api using google apps script and sheet.任何人都可以帮助我了解如何使用谷歌应用程序脚本和工作表将图像发送到 api。
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 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命令时,如下所示。
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
将请求正文发送{"logo": "data"}
到“files”。multipart/form-data
, it is required to create the request body.multipart/form-data
发送文件数据,需要创建请求正文。
In your script,在你的脚本中,
"Authorization":"Enter Token here"
."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."Content-Type":"application/json"
。 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');
UrlFetchApp.fetch('Enter URL Here');
returns the binary data of the image of PNG or JPEG.data
is important.data
中的换行符很重要。 So please be careful this.authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY)
as the authorization header.authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY)
作为授权标头。 But if API_SECRET_KEY
is the same with "Basic " + Utilities.base64Encode(API_SECRET_KEY)
, please try to modify to authorization : "API_SECRET_KEY"
.API_SECRET_KEY
与"Basic " + Utilities.base64Encode(API_SECRET_KEY)
,请尝试修改为authorization : "API_SECRET_KEY"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.