Square Connect API-图片上传-Node.js [英] Square Connect API - image upload - Node.js

查看:148
本文介绍了Square Connect API-图片上传-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天的时间尝试使用Square Connect v1 API使用image_upload端点成功上传图像. API文档在此处

I've spent days trying to successfully upload images with the image_upload endpoint using the Square Connect v1 API. The API docs are here

当前,我在进行POST后收到以下响应.

Currently, I'm getting the following response after making the POST.

{"type":"bad_request","message":"Could not create image"}

我正在这样使用节点请求库:

I'm using the node-request library as such:

const formData = {
      image_data: {
        value: BASE64IMAGE,
        options: {
          'Content-Disposition': 'form-data',
          filename: 'hat.jpg',
          'Content-Type': 'image/jpeg',
        },
      },
    };
    request.post(
      {
        method: 'POST',
        url: `https://connect.squareup.com/v1/${location}/items/${item_id}/image`,
        headers: {
          Authorization: `Bearer ${access_token}`,
          'Content-Type': 'multipart/form-data; boundary=BOUNDARY',
          Accept: 'application/json',
        },
        formData,
      },
      (err, httpResponse, body) => {
        if (err) {
          return console.error('upload failed:', err);
        }
        console.log('Upload successful!  Server responded with:', body);
      },

有没有人能够通过node.js成功使用此端点?

Has anybody out there been able to use this endpoint successful using node.js?

推荐答案

经过几天的玩耍,我终于使它工作了.尽管最后我没有将图像保存到磁盘上,然后将其发布到Square上,还是无法使它工作.这是我的工作片段:

After many days of playing around, I finally got it working. Although, in the end, I was not able to make it work without first saving the image to disk, and then posting it to Square. Here is my working snippet:

let mimeOptions = {
 'Content-Disposition': 'form-data',
 filename: 'photo.jpg',
 'Content-Type': 'image/jpeg',
};
if (type === 'png') {
 mimeOptions = {
  'Content-Disposition': 'form-data',
  filename: 'photo.png',
  'Content-Type': 'image/png',
 };
}

const options = {
 url: shopifyImage.src,
 dest: `${__dirname}/temp/${uuid()}.${type}`,
};

download
 .image(options)
 .then(({ filename, image }) => {
  const formData = {
    image_data: {
      value: fs.createReadStream(filename),
      options: mimeOptions,
    },
  };
  request.post(
    {
      method: 'POST',
      url: `https://connect.squareup.com/v1/${squareCredentials.location_id}/items/${
        squareProduct.catalog_object.id
      }/image`,
      headers: {
        Authorization: `Bearer ${squareCredentials.access_token}`,
        'Content-Type': 'multipart/form-data; boundary=BOUNDARY',
        Accept: 'application/json',
      },
      formData,
    },
    (err, httpResponse, body) => {
      fs.unlink(filename, () => {});
      if (err) {
        return console.error('upload failed:', err);
      }
    },
  );
})
.catch((err) => {
  console.error(err);
});

这篇关于Square Connect API-图片上传-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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