[英]Read Images from system or url using google apps script in google sheets and post image to an API
我是新來的。
我正在嘗試使用 put 請求上傳圖像來訪問 api,我想使用谷歌表格和谷歌應用程序腳本來完成。 我可以將圖像從 url 添加到工作表,但不能添加到 api,因為它只接受 .png/.jpeg 格式,我認為目前它采用 blob 格式。 沒有把握。
任何人都可以幫助我了解如何使用谷歌應用程序腳本和工作表將圖像發送到 api。
當我在參數中輸入圖像作為密鑰(文件類型)然后從設備上傳圖像時,我能夠使用郵遞員獲得成功響應。
這是我用來實現上述目的的一些代碼:
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
有人請幫忙
我相信你的目標如下。
當我看到“更新事件徽標(海報)”的示例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'
multipart/form-data
將請求正文發送{"logo": "data"}
到“files”。multipart/form-data
發送文件數據,需要創建請求正文。
在你的腳本中,
"Authorization":"Enter Token here"
。 但是從示例 curl 命令中,我認為“API_SECRET_KEY”不是用 base64 編碼的。"Content-Type":"application/json"
。當你的腳本被修改時,它變成如下。
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');
返回 PNG 或 JPEG 圖像的二進制數據。 請注意這一點。data
中的換行符很重要。 所以請注意這一點。authorization : "Basic " + Utilities.base64Encode(API_SECRET_KEY)
作為授權標頭。 但是如果API_SECRET_KEY
與"Basic " + Utilities.base64Encode(API_SECRET_KEY)
,請嘗試修改為authorization : "API_SECRET_KEY"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.