首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Square Connect接口-镜像上传- Node.js

Square Connect接口-镜像上传- Node.js
EN

Stack Overflow用户
提问于 2018-07-11 23:23:36
回答 2查看 122关注 0票数 0

我花了几天的时间尝试使用Square Connect v1应用程序接口成功地通过image_upload端点上传图像。The API docs are here

目前,我在写完这篇文章后得到了以下回复。

代码语言:javascript
复制
{"type":"bad_request","message":"Could not create image"}

我使用的是node-request库:

代码语言:javascript
复制
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成功地使用这个端点?

EN

回答 2

Stack Overflow用户

发布于 2018-07-12 05:20:36

出现错误的原因是您需要提供二进制图像数据,但您提供的是base64编码的数据。你可以在here上看到一个如何做的例子。

票数 0
EN

Stack Overflow用户

发布于 2018-07-13 05:36:36

在玩了很多天之后,我终于把它弄好了。尽管,最后,如果不先将图像保存到磁盘,然后将其发布到Square,我就无法使其工作。下面是我的工作片段:

代码语言:javascript
复制
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);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51289166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档