[英]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.