简体   繁体   English

使用谷歌表格中的谷歌应用脚​​本从系统或网址读取图像并将图像发布到 API

[英]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.我相信你的目标如下。

Modification points:改装要点:

  • 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'
    • In this case, the basic authorization is used.在这种情况下,使用基本授权。 And it seems that the request body is required to be sent {"logo": "data"} to "files" using multipart/form-data .并且似乎需要使用multipart/form-data将请求正文发送{"logo": "data"}到“files”。
    • In order to send the file data with multipart/form-data , it is required to create the request body.为了使用multipart/form-data发送文件数据,需要创建请求正文。
      • In this case, the request body is created with the byte array, because the binary data is included.在这种情况下,请求正文是使用字节数组创建的,因为包含二进制数据。
  • In your script,在你的脚本中,

    • I cannot understand about "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.但是从示例 curl 命令中,我认为“API_SECRET_KEY”不是用 base64 编码的。
    • At the sample curl command, "Content-Type":"application/json" is not used.在示例 curl 命令中,未使用"Content-Type":"application/json"

Modified script:修改后的脚本:

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);
  • In my environment, I could confirm that the request of above modified script is the same with that of the curl command.在我的环境中,可以确认上述修改脚本的请求与 curl 命令的请求相同。
  • In this case, it supposes that UrlFetchApp.fetch('Enter URL Here');在这种情况下,它假设UrlFetchApp.fetch('Enter URL Here'); returns the binary data of the image of PNG or JPEG.返回 PNG 或 JPEG 图像的二进制数据。 Please be careful this.请注意这一点。

Note:笔记:

  • Please use this script with enabling V8 runtime.请在启用 V8 运行时使用此脚本。
  • In this case, the line breaks in data is important.在这种情况下, data中的换行符很重要。 So please be careful this.所以请注意这一点。
  • When your "API_SECRET_KEY" and URL are not correct, an error occurs.当您的“API_SECRET_KEY”和 URL 不正确时,就会发生错误。 Please be careful this.请注意这一点。
  • From the sample curl command, I proposed authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY) as the authorization header.从示例 curl 命令中,我建议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"

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM