[英]eBay API - UploadSiteHostedPictures - Python
我在尝试将图像上传到 Python 中的 Ebay 时遇到问题。 I have never had a problem via VBA in Excel listing items for over 6 years following the official Ebay PHP example but cannot get it to work in Python.
我不断收到“图片服务仅支持上传 JPEG、GIF、PNG、BMP 和 TIFF 图像格式。请使用以其中一种格式保存的图片版本重试。” 尽管图像是 jpg 并且可以通过我的 VBA 方法上传。
我已经阅读并调整了 3 天的请求,但无济于事。 我打赌这很简单,所以我希望有人能指出我的错误或提供一个完整的工作示例。
更改版本并没有什么不同,571 仍然适用于 VBA 实现。
我向可以帮助我完成这项工作的人捐款 Paypal 没有问题。
提前致谢。
with open(r"H:\temp\earth.jpg", "rb") as image_file:
encoded_string = (base64.encodebytes(image_file.read())).decode("utf-8")
mimeBoundary = 'MIME_boundary'
ebayAuthToken = '<token>'
requestHeaders = {
'X-EBAY-API-COMPATIBILITY-LEVEL': '1113',
'X-EBAY-API-SITEID': '15',
'X-EBAY-API-DEV-NAME': '<devName>',
'X-EBAY-API-APP-NAME': '<appName>',
'X-EBAY-API-CERT-NAME': '<certName>',
'X-EBAY-API-CALL-NAME': 'UploadSiteHostedPictures',
'Content-Type': 'multipart/form-data; boundary=' + mimeBoundary
}
xmlRequest = (
'<?xml version="1.0" encoding="utf-8"?>'
'<UploadSiteHostedPicturesRequest xmlns="urn:ebay:apis:eBLBaseComponents">'
'<RequesterCredentials>'
f'<eBayAuthToken>{ebayAuthToken}</eBayAuthToken>'
'</RequesterCredentials>'
'<PictureSet>Supersize</PictureSet>'
'<Version>517</Version>>'
'</UploadSiteHostedPicturesRequest>'
)
firstPart = ''
firstPart += '--' + mimeBoundary + '\r\n'
firstPart += 'Content-Disposition: form-data; name=""XML Payload"' + '\r\n'
firstPart += 'Content-Type: text/xml;charset=utf-8' + '\r\n\r\n'
firstPart += f'{xmlRequest}'
firstPart += '\r\n\r\n'
secondPart += '--' + mimeBoundary + '\r\n'
secondPart += 'Content-Disposition: form-data; name=""dummy""; filename=""dummy"' + '\r\n'
secondPart += 'Content-Transfer-Encoding: binary' + '\r\n'
secondPart += 'Content-Type: application/octet-stream' + '\r\n\r\n'
secondPart += f'{encoded_string}' # image binary data
secondPart += '\r\n'
secondPart += '--' + mimeBoundary + '--' + '\r\n'
fullRequest = firstPart + secondPart
uploadImageResponse = requests.post('https://api.ebay.com/ws/api.dll', data=fullRequest, headers=requestHeaders, verify=False)
我为将来遇到此问题的任何人找到了解决方案。 请求部分需要通过将每个部分编码为字节来连接。 见下文:
tmpfile = 'H:/temp/%s.bin' % random.randint(0, 100000)
f = open(tmpfile, 'wb')
f.write(firstPart.encode())
f.write(secondPart.encode())
f.write(base64.b64decode(encoded_string))
f.write(CRLF.encode())
f.write(("--" + mimeBoundary + "--" + CRLF).encode())
f.close()
# read back what we wrote to the file
f = open(tmpfile, 'rb')
fullRequest = f.read()
f.close()
请注意,您不需要将它们写入文件并将其读取到文件中,这就是我找到的解决方案的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.