Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在google sheets中使用google apps脚本从系统或url读取图像,并将图像发布到API_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 在google sheets中使用google apps脚本从系统或url读取图像,并将图像发布到API

Javascript 在google sheets中使用google apps脚本从系统或url读取图像,并将图像发布到API,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我是新来的 我正在尝试使用put请求来点击一个api来上传图像,我想使用GoogleSheets和GoogleApps脚本来实现这一点。我可以将url中的图像添加到工作表中,但不能添加到api中,因为它只接受.png/.jpeg格式,而且我认为目前它将采用blob格式。不确定 任何人都可以帮助我如何发送图像到api使用谷歌应用程序脚本和工作表 当我在参数中键入image作为键(filetype),然后从设备上载图像时,我可以使用postman获得成功响应 以下是我用来实现上述目标的一些代码: f

我是新来的

我正在尝试使用put请求来点击一个api来上传图像,我想使用GoogleSheets和GoogleApps脚本来实现这一点。我可以将url中的图像添加到工作表中,但不能添加到api中,因为它只接受.png/.jpeg格式,而且我认为目前它将采用blob格式。不确定

任何人都可以帮助我如何发送图像到api使用谷歌应用程序脚本和工作表

当我在参数中键入image作为键(filetype),然后从设备上载图像时,我可以使用postman获得成功响应

以下是我用来实现上述目标的一些代码:

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/#更新活动徽标海报


有人请帮忙

我相信你的目标如下

  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'
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);
  • 您想请求使用Google Apps脚本
修改点:
  • 当我看到“updateeventlogo(Poster)”的示例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'
    
    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);
    
    • 在我的环境中,我可以确认上面修改的脚本的请求与curl命令的请求相同
    • 在本例中,它假定
      UrlFetchApp.fetch('enterurl Here')
      返回PNG或JPEG图像的二进制数据。请小心这个
    注:
    • 请在启用V8运行时时使用此脚本
    • 在这种情况下,
      数据中的换行符非常重要。所以请小心这个
    • 如果“API\u密钥”和URL不正确,则会发生错误。请小心这个
    • 从示例curl命令中,我建议使用
      授权:“Basic”+Utilities.base64Encode(API\u SECRET\u KEY)
      作为授权头。但是如果
      API\u SECRET\u KEY
      “Basic”+实用程序相同。base64Encode(API\u SECRET\u KEY)
      ,请尝试修改为
      授权:“API\u SECRET\u KEY”
    参考:

      • 我相信你的目标如下

          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'
        
        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);
        
        • 您想请求使用Google Apps脚本
        修改点:
        • 当我看到“updateeventlogo(Poster)”的示例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'
          
          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);
          
          • 在我的环境中,我可以确认上面修改的脚本的请求与curl命令的请求相同
          • 在本例中,它假定
            UrlFetchApp.fetch('enterurl Here')
            返回PNG或JPEG图像的二进制数据。请小心这个
          注:
          • 请在启用V8运行时时使用此脚本
          • 在这种情况下,
            数据中的换行符非常重要。所以请小心这个
          • 如果“API\u密钥”和URL不正确,则会发生错误。请小心这个
          • 从示例curl命令中,我建议使用
            授权:“Basic”+Utilities.base64Encode(API\u SECRET\u KEY)
            作为授权头。但是如果
            API\u SECRET\u KEY
            “Basic”+实用程序相同。base64Encode(API\u SECRET\u KEY)
            ,请尝试修改为
            授权:“API\u SECRET\u KEY”
          参考:

          为了正确了解您当前的情况,您能否提供您想要使用的API的规范和当前的错误消息?API显示不支持的文件类型。所以我假设我的代码正在发送blob。API接受图像密钥中的png文件谢谢您的回复。例如,当您将文件作为字节数组而不是blob发送时,会得到什么结果?它类似于
          var-formData=Utilities.newBlob(二进制数据,'image/png','MyImageName').getBytes()
          var-formData=JSON.stringify({image:Utilities.newBlob(二进制数据,'image/png','MyImageName').getBytes())
          。如果这些不是直接的解决方案,我很抱歉。虽然我仍然无法理解您要使用的API的规格,但从
          中,API说不支持文件类型,例如,base64如何?API只接受png和jpeg格式的图像,不接受base64或blob等其他文件格式或格式。错误-{“代码”:415,“消息”:“HTTP 415不支持的媒体类型”}为了正确了解您当前的情况,您能否提供要使用的API的规范和当前错误消息?API显示不支持的文件类型。所以我假设我的代码正在发送blob。API接受图像密钥中的png文件谢谢您的回复。例如,当您将文件作为字节数组而不是blob发送时,会得到什么结果?它类似于
          var-formData=Utilities.newBlob(二进制数据,'image/png','MyImageName').getBytes()
          var-formData=JSON.stringify({image:Utilities.newBlob(二进制数据,'image/png','MyImageName').getBytes())
          。如果这些不是直接的解决方案,我很抱歉。虽然我仍然无法理解您要使用的API的规格,但从
          中,API说不支持文件类型,例如,base64如何?API只接受png和jpeg格式的图像,不接受base64或blob等其他文件格式或格式。错误-{“代码”:415,“消息”:“HTTP 415不支持的媒体类型”}嘿,谢谢你的回答,但有一个问题。当我尝试使用包含图像的google驱动器链接时,会出现以下异常:返回代码400的请求失败。截断的服务器响应:{“code”:400,“message”:“命令失败:convert-im6.q16:未授权”`/tmp/uploadBeb5ea5532e2663e047ac3a813acb175'@error/compose.c/ReadImage…(使用muteHttpExceptions选项检查完整响应)(第30行,文件“code”)@Ponnay感谢您的回复。对于给您带来的不便,我深表歉意。从您的错误消息中,我们认为错误的原因可能是授权和/或URL。在我建议的回答中,请求与off的示例curl命令相同