eBay API-UploadSiteHostedPictures-Python

问题描述 投票:0回答:1

我在尝试使用Python将图像上传到Ebay时遇到问题。在正式的Ebay PHP示例之后的6年中,我从未通过VBA在Excel列出项目中遇到问题,但无法使其在Python中工作。

我一直在收到“ Picture Services仅支持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)
python image python-requests mime ebay-api
1个回答
0
投票

我为以后遇到此问题的任何人找到了解决方案。需要通过将每个请求部分编码为字节来加入请求部分。见下文:

        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()

注意,您不需要将它们写入和读取到文件中,这就是我找到的解决方案的方法。

© www.soinside.com 2019 - 2024. All rights reserved.