如何从 Google 驱动器创建图像文件并将其插入到 Google Apps 脚本 UrlFetchApp.fetch 调用外部 API?
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?
我正在使用 Google Apps 脚本调用外部 API 到 post 文本和图像到外部系统中的联系人。我已经 post 很好地编辑了文本很多次,没问题。我以前没有在 Apps 脚本中发送过甚至使用过图像,所以我不确定如何将图像作为文件发送。我对 Stack Overflow 和其他地方做了很多研究,但还没有找到答案。
外部系统的 API 文档说它需要以下内容:
contactId - 类型:字符串
留言:
文本 - 类型:字符串... 描述:消息文本(需要媒体或文本)。
上传 - 类型:文件... 描述:消息图像(需要媒体或文本)。媒体必须小于 1.5mb。使用 jpg、jpeg、png 或 gif。
"upload",类型 "File"(一个 jpg picture/image)是我不知道如何抓取、格式化和发送的内容。我目前在 Google 驱动器中有图像,已将其共享给任何人通过其 URL 访问,并且它小于 1.5MB。
这是我的大部分测试代码(标记为 JS,但实际上是 Google Apps 脚本),识别信息已更改,我尝试了几种不同的方法。在这一点上,我只是在用头撞墙!任何帮助是极大的赞赏!!!谢谢!
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);
}
再一次,除了图像("upload")没有通过之外,一切正常(文本等)。我很确定这是因为我不知道如何 "package" 来自 Google 的图像通过 UrlFetchApp 调用驱动到 API.
官方文档说的是Message text (Media or Text is required)
和Message image (Media or Text is required)
。由此,请尝试按以下修改进行测试。
修改后的请求正文:
var formData = {
upload: DriveApp.getFileById("###").getBlob()
};
- 我以为从官方文档看,
'upload'
和'text'
都用的时候,可能只有'text'
才用
- 另外,从你的测试结果来看,请求体需要作为表单数据发送。
参考:
我正在使用 Google Apps 脚本调用外部 API 到 post 文本和图像到外部系统中的联系人。我已经 post 很好地编辑了文本很多次,没问题。我以前没有在 Apps 脚本中发送过甚至使用过图像,所以我不确定如何将图像作为文件发送。我对 Stack Overflow 和其他地方做了很多研究,但还没有找到答案。
外部系统的 API 文档说它需要以下内容:
contactId - 类型:字符串
留言:
文本 - 类型:字符串... 描述:消息文本(需要媒体或文本)。
上传 - 类型:文件... 描述:消息图像(需要媒体或文本)。媒体必须小于 1.5mb。使用 jpg、jpeg、png 或 gif。
"upload",类型 "File"(一个 jpg picture/image)是我不知道如何抓取、格式化和发送的内容。我目前在 Google 驱动器中有图像,已将其共享给任何人通过其 URL 访问,并且它小于 1.5MB。
这是我的大部分测试代码(标记为 JS,但实际上是 Google Apps 脚本),识别信息已更改,我尝试了几种不同的方法。在这一点上,我只是在用头撞墙!任何帮助是极大的赞赏!!!谢谢!
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);
}
再一次,除了图像("upload")没有通过之外,一切正常(文本等)。我很确定这是因为我不知道如何 "package" 来自 Google 的图像通过 UrlFetchApp 调用驱动到 API.
官方文档说的是Message text (Media or Text is required)
和Message image (Media or Text is required)
。由此,请尝试按以下修改进行测试。
修改后的请求正文:
var formData = {
upload: DriveApp.getFileById("###").getBlob()
};
- 我以为从官方文档看,
'upload'
和'text'
都用的时候,可能只有'text'
才用 - 另外,从你的测试结果来看,请求体需要作为表单数据发送。