我花了几天的时间尝试使用Square Connect v1应用程序接口成功地通过image_upload端点上传图像。The API docs are here
目前,我在写完这篇文章后得到了以下回复。
{"type":"bad_request","message":"Could not create image"}我使用的是node-request库:
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成功地使用这个端点?
发布于 2018-07-12 05:20:36
出现错误的原因是您需要提供二进制图像数据,但您提供的是base64编码的数据。你可以在here上看到一个如何做的例子。
发布于 2018-07-13 05:36:36
在玩了很多天之后,我终于把它弄好了。尽管,最后,如果不先将图像保存到磁盘,然后将其发布到Square,我就无法使其工作。下面是我的工作片段:
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);
});https://stackoverflow.com/questions/51289166
复制相似问题