基于Product ID更新Stripe商品图片的Google Apps Script代码报错修复
问题:调用Stripe API更新商品图片时返回"Invalid array"错误
我尝试通过Google Apps Script调用Stripe API,根据表格中的Product ID批量更新商品图片。已创建包含以下数据的Google表格:
| ProductId | ImageUrl |
|---|---|
| prod_PV3k2gGM3GrmqO | https://img.freepik.com/free-vector/sketch-plain-travelling_1308-86639.jpg |
| prod_PV3kLlPyxmwcy7 | https://img.freepik.com/free-vector/sketch-plain-travelling_1308-86639.jpg |
| prod_PV3kubG9uCMsFR | https://img.freepik.com/free-vector/sketch-plain-travelling_1308-86639.jpg |
编写的Google Apps Script代码如下:
// Define your Stripe API key here const STRIPE_API_KEY = 'hidden'; function insertImageIntoStripeProduct() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const data = sheet.getDataRange().getValues(); for (let i = 1; i < data.length; i++) { const productId = data[i][0]; // Assuming ProductID is in the first column const imageUrl = data[i][1]; // Assuming ImageURL is in the second column if (productId && imageUrl) { updateStripeProduct(productId, imageUrl); } } } function updateStripeProduct(productId, imageUrl) { const apiUrl = `https://api.stripe.com/v1/products/${productId}`; const productData = { images: [imageUrl], }; makeStripeRequest(apiUrl, 'POST', productData); } function makeStripeRequest(apiUrl, method, requestData) { const formData = Object.entries(requestData) .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) .join('&'); const response = UrlFetchApp.fetch(apiUrl, { method, headers: { Authorization: `Bearer ${STRIPE_API_KEY}`, 'Content-Type': 'application/x-www-form-urlencoded', }, payload: formData, }); if (response.getResponseCode() !== 200) { throw new Error(`Stripe API Error: ${response.getContentText()}`); } return JSON.parse(response.getContentText()); }
执行代码后出现以下错误:
Exception: Request failed for https://api.stripe.com returned code 400. Truncated server response: { "error": { "message": "Invalid array", "param": "images", "request_log_url": "https://dashboard.stripe.com/logs/req_Ux1hF7z4QsOtZ1?... (use muteHttpExceptions option to examine full response) makeStripeRequest @ Code.gs:32 updateStripeProduct @ Code.gs:24 insertImageIntoStripeProduct @ Code.gs:13
解决方案
错误原因是Stripe的x-www-form-urlencoded格式要求数组类型的参数必须以key[]的形式传递,而原代码直接将数组转为images=xxx,不符合API的格式要求。
修改后的完整代码如下:
// Define your Stripe API key here const STRIPE_API_KEY = 'hidden'; function insertImageIntoStripeProduct() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const data = sheet.getDataRange().getValues(); for (let i = 1; i < data.length; i++) { const productId = data[i][0]; const imageUrl = data[i][1]; if (productId && imageUrl) { updateStripeProduct(productId, imageUrl); } } } function updateStripeProduct(productId, imageUrl) { const apiUrl = `https://api.stripe.com/v1/products/${productId}`; const productData = { images: [imageUrl], }; makeStripeRequest(apiUrl, 'POST', productData); } function makeStripeRequest(apiUrl, method, requestData) { const formEntries = []; Object.entries(requestData).forEach(([key, value]) => { if (Array.isArray(value)) { // 处理数组类型参数,生成key[]=value的格式 value.forEach(item => { formEntries.push(`${encodeURIComponent(key)}[]=${encodeURIComponent(item)}`); }); } else { // 处理普通参数 formEntries.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); } }); const formData = formEntries.join('&'); const response = UrlFetchApp.fetch(apiUrl, { method, headers: { Authorization: `Bearer ${STRIPE_API_KEY}`, 'Content-Type': 'application/x-www-form-urlencoded', }, payload: formData, muteHttpExceptions: true // 开启后可以获取完整错误信息,方便调试 }); const responseCode = response.getResponseCode(); if (responseCode !== 200) { throw new Error(`Stripe API Error (${responseCode}): ${response.getContentText()}`); } return JSON.parse(response.getContentText()); }
关键修改点:
- 数组参数处理:在
makeStripeRequest函数中,判断值是否为数组,如果是则生成key[]=value的格式,每个数组元素单独编码后加入表单数据。 - 调试优化:添加
muteHttpExceptions: true,可以获取完整的错误响应内容,便于排查问题。
内容的提问来源于stack exchange,提问作者Roman Patelev
相关产品推荐
相关产品推荐

