如何创建图像,然后将来自Google云端硬盘的图像文件插入对外部API的Google Apps脚本UrlFetchApp.fetch调用中? [英] How do you create and then insert an image file from Google Drive into a Google Apps Script UrlFetchApp.fetch call to an External API?

查看:77
本文介绍了如何创建图像,然后将来自Google云端硬盘的图像文件插入对外部API的Google Apps脚本UrlFetchApp.fetch调用中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本调用外部API,以将文本和图像发布到外部系统中的联系人.我已经很好地发布了文字,很多次,没有问题.之前,我还没有在Apps脚本中发送或使用图像的经验,因此我不确定如何将图像作为文件发送.我已经对Stack Overflow和其他地方做了很多研究,但是还没有找到答案.

I am using Google Apps Script to call an external API to post both text and an image to a contact in an external system. I have posted the text fine, many times, no problems. I have not worked with sending or even using images in Apps Script before, so I am unsure of how to send the image as a file. I've done quite a bit of research on Stack Overflow and elsewhere, but have not found the answer to this yet.

外部系统的API文档说,它需要满足以下条件:

The API documentation for the external system says that it needs the following:

contactId-类型:字符串

contactId - Type: String

消息:

文本-类型:字符串...说明:消息文本(需要媒体或文本).

text - Type: String... Description: Message text (Media or Text is required).

上载-类型:文件...说明:消息图像(需要媒体或文本).媒体必须小于1.5mb.使用jpg,jpeg,png或gif.

upload - Type: File... Description: Message image (Media or Text is required). Media must be smaller than 1.5mb. Use a jpg, jpeg, png, or gif.

我无法弄清楚如何抓取,格式化和发送上传",键入文件"(jpg图片/图像).我目前在Google云端硬盘中保存了该图片,并已将其共享给所有人,并可以通过其URL访问,并且该图片的大小不到1.5MB.

The "upload", type "File" (a jpg picture/image) is what I cannot figure out how to grab, format, and send. I currently have the image in Google Drive, have shared it for anyone to access via its URL, and it is well under 1.5MB.

这是我大部分的测试代码(标记为JS,但实际上是Google Apps脚本),其中的标识信息已更改,我尝试了几种不同的方法.在这一点上,我只是把头撞在墙上!任何帮助是极大的赞赏!!!谢谢!

Here is most of my test code (marked as JS, but really Google Apps Script), with the identifying info changed, with several different ways I have tried it. At this point, I am just banging my head against the wall! Any help is greatly appreciated!!! Thank you!

function TestAPI() {
  
  var apiKey2 = '9xxxxx-xxxx2-xxxxx-bxxx-3xxxxxxa'; //API Key for the external system
  
  var url4 = 'https://www.externalsystem.com/api/v1/[contactID]/send';
  var pic = DriveApp.getFileById("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_");  // I Tried this
  //  var pic = driveService.files().get('1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_');  //And tried this
  //  var pic = DriveApp.getFileByID('1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_').getAs(Family.JPG);  //And this
  //  var pic = { "image" : { "source": {"imageUri": "https://drive.google.com/file/d/1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_" } } };  //And this
  //  var pic = { file : DriveApp.getFileById("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_") };  //And this
  
  var formData = {
    'contactID': '[contactID]',
    'text': "Text here to send to external system through API",   // This works fine every time!
    'upload': pic       // Tried this
//  'upload': DriveApp.getFileByID("1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_").getBlob()  // And tried this
//  'upload': { "image" : { "source": {"imageUri": "https://drive.google.com/file/d/1yyyyyy_Nyyyyxxxxxx-_xxxxxxx_xyyyy_" } } }  // And this
  };
  
  var options4 = {
    "headers":{"x-api-key":apiKey2},
    'method' : 'post',
    'payload': formData
  };
  
  var response4 = UrlFetchApp.fetch(url4, options4);

}

再一次,一切正常(文本等),但没有通过图像(上传").我非常确定这是因为我不知道如何通过对API的UrlFetchApp调用将Google云端硬盘中的图片打包".

Once again, everything is working fine (text, etc.) except for the image (the "upload") not coming through. I am pretty sure it is because I don't know how to "package" the image from Google Drive through the UrlFetchApp call to the API.

推荐答案

官方文件说Message text (Media or Text is required)Message image (Media or Text is required).从此,请尝试按以下修改进行测试.

The official document says that Message text (Media or Text is required) and Message image (Media or Text is required). From this, please try to test as following modification.

var formData = {
  upload: DriveApp.getFileById("###").getBlob()
};

  • 我认为从官方文档来看,当同时使用'upload''text'时,只能使用'text'.
  • 而且,从您的测试结果中发现,请求正文需要作为表单数据发送.
    • I thought that from the official document, when both 'upload' and 'text' are used, only 'text' might be used.
    • And also, from your tested result, it was found that the request body is required to be sent as the form data.
    • 这篇关于如何创建图像,然后将来自Google云端硬盘的图像文件插入对外部API的Google Apps脚本UrlFetchApp.fetch调用中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆