我正在try 使用条纹API来更新基于产品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的操作脚本中有以下代码:

// 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

你能帮我修改这段代码吗,让它正常工作?

提前谢谢您.

最好的,

罗马人

我试过使用ChatGPT.它提供了数十次代码修改,但都没有奏效.

推荐答案

当我看到您提供的URL和显示脚本时,我猜您可能想要使用Update a product.如果我的理解是正确的,似乎在images的情况下,值是[].

在本例中,如果有多个查询参数,如images="http://###"&images="http://###",则请求以images=["http://###","http://###"]运行.但是,在像images="http://###"这样的单个查询参数的情况下,我认为请求是使用images="http://###"运行的.我想你的 playbook 也是一样的.但是,在您的情况下,会出现错误.在"images"处出现错误"Invalid array".

基于以上情况,据我猜测,我想提出以下4种修改模式.请帮我测试一下.

模式一:

From:

images: [imageUrl],

To:

"images[]": imageUrl,
  • 对于"images[]": imageUrl,,imagesimages%5B%5D=http%3A%2F%2F###.是images[]=http://###.

模式2:

From:

images: [imageUrl],

To:

images: JSON.stringify([imageUrl]),
  • 对于images: JSON.stringify([imageUrl]),,imagesimages=%5B%22http%3A%2F%2F###%22%5D.是images=["http://###"].

模式3:

From:

images: [imageUrl],

To:

images: `[${imageUrl}]`,
  • images: [${ImageUrl}],的情况下,imagesimages=%5Bhttp%3A%2F%2Faaa%5D.是images=[http://aaa].

模式4:

From:

const formData = Object.entries(requestData)
  .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
  .join('&');

To:

const formData = Object.entries(requestData)
  .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
  .join('&') + "&images";
  • 在此模式中,请不要修改images: [imageUrl],,并按如下方式修改您的脚本.
  • images: [imageUrl],.join('&') + "&images";而言,imagesimages=http%3A%2F%2F###&images.是images=http://###&images.

注:

  • 在这些修改中,它假设您的URL和您对Authorization: Bearer ${STRIPE_API_KEY}的授权有效.请注意这一点.

Javascript相关问答推荐

使用json文件字符串来enum显示类型字符串无法按照计算的enum成员值的要求分配给类型号

模块与独立组件

我开始使用/url?q=使用Cheerio

如何在mongoose中链接两个模型?

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

还原器未正确更新状态

如何在文本字段中输入变量?

单个HTML中的多个HTML文件

如何发送从REST Api收到的PNG数据响应

如何在Angular拖放组件中同步数组?

使用带有HostBinding的Angular 信号来更新样式?

Reaction-SWR-无更新组件

如何在Press上重新启动EXPO-AV视频?

如何用javascript更改元素的宽度和高度?

基于产品ID更新条带产品图像的JavaScript命中错误

用Reaction-RT-Chart创建实时条形图

如果我的列有条件,我如何呈现图标?

在范围数组中查找公共(包含)范围

无法在Adyen自定义卡安全字段创建中使用自定义占位符

如何缩小函数中联合返回类型的范围