我正在尝试使用他们的FileTransfer API将批量数据交换调用上传到eBay。
为了做到这一点,我必须将xml请求字符串POST到eBay服务器但是xml
请求包含一个<Data>
部分,其中必须包含您要传输的数据(在这种情况下,它是包含另一个xml文档的base64编码的zipfile)。 eBay在C#中举了一个如何构建这样一个文档的例子:https://ebay.custhelp.com/app/answers/detail/a_id/1561
我一直在尝试在python中使用httplib来重新创建这个示例,以便以与示例相同的方式POST一个我构造的字符串(3个UUID是唯一的):
request = """
--MIMEBoundaryurn_uuid_{XMLUUID}
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml;charset=UTF-8";
Content-Transfer-Encoding: binary
Content-ID:<0.urn:uuid:{REQUUID}>
<?xml version="1.0" encoding="utf-8"?>
<uploadFileRequest xmlns:sct=\"http://www.ebay.com/soaframework/common/types\" xmlns="http://www.ebay.com/marketplace/services">
<fileAttachment>
<Size>{Size}</Size>
<Data><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include"
href="cid:urn:uuid:{ATTCHMNTUUID}>"</Data>
</fileAttachment>
<fileFormat>{fileFormat}</fileFormat>
<fileReferenceId>{fileReferenceId}</fileReferenceId>
<taskReferenceId>{taskReferenceId}</taskReferenceId>
</uploadFileRequest>
--MIMEBoundaryurn_uuid_{XMLUUID}
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <urn.uuid:{ATTCHMNTUUID}>\r\n
{Data}
--MIMEBoundaryurn_uuid_{XMLUUID}--
""".replace("\t", "")
request_dict = {
'Size': size,
'Data': payload,
'fileFormat': 'zip',
'fileReferenceId': '50000935383',
'taskReferenceId': '50000847753',
'REQUUID': reqUUID,
'XMLUUID': xmlUUID,
'ATTCHMNTUUID': attchmntUUID,
}
request = request.format( **request_dict )
标题如下所示:
headers = {
'X-EBAY-SOA-OPERATION-NAME': 'uploadFile',
'X-EBAY-SOA-SERVICE-NAME': 'FileTransferService',
'X-EBAY-SOA-SECURITY-TOKEN': #Auth Token,
'Content-type': "multipart/related; boundary=" + boundary + ";type=\"application/xop+xml\";start=\"<0." + "urn:uuid:" + str(requuid) + ">\";start-info=\"text/xml\""
}
然后我的POST:
connection = httplib.HTTPSConnection( 'storage.sandbox.ebay.com' )
connection.request( "POST", '/FileTransferService', request, headers )
当我发布没有MIME附件信息的xml请求时,它接受该文件没有任何问题。但是,当我尝试像上面的代码一样,使用MIME多部分/相关附件和<xop>
标记指出数据在附件中的位置时,POST不成功,我得到一个“错误302 :暂时移动“回复。这让我相信我构建MIME多部分/相关xml请求的方式不对,或者我是如何构造“Content-type”头部声明,或者我使用xop,或者很可能是三者的组合的东西。
我想我的问题是:如何创建包含MIME多部分/相关部分的xml请求并使用xop?
感谢您的帮助!
韦斯
答案 0 :(得分:2)
我弄清楚我的要求出了什么问题。它是构造请求字符串和标题的组合。我采用了更加有组织和程序化的方法,并且有效。
以下是构建请求字符串的部分:
###########################################
# UUIDs
###########################################
reqUUID= uuid.uuid4()
attchmntUUID = uuid.uuid4()
##########################################
# MIME strings
##########################################
URN_UUID_REQUEST = "<0.urn:uuid:%s>"% reqUUID
URN_UUID_ATTACHMENT = "urn:uuid:%s" % attchmntUUID
MIME_BOUNDARY = "MIME_boundary"
request_dict = {
'Size': size,
'Data': payload,
'fileFormat': 'gzip',
'fileReferenceId': '50000945773',
'taskReferenceId': '50000858033',
'REQUUID': reqUUID,
'ATTCHMNTUUID': attchmntUUID,
}
def build_request( request_dict):
'''
Build the request string with MIME Attachment
'''
request = '<uploadFileRequest xmlns:sct="http://www.ebay.com/soaframework/common/types" xmlns="http://www.ebay.com/marketplace/services">\r\n'
request += '<taskReferenceId>%s</taskReferenceId>\r\n' % request_dict['taskReferenceId']
request += '<fileReferenceId>%s</fileReferenceId>\r\n' % request_dict['fileReferenceId']
request += '<fileFormat>%s</fileFormat>\r\n' % request_dict['fileFormat']
request += '<fileAttachment>\r\n'
request += '<Size>%s</Size>\r\n'% request_dict['Size']
request += '<Data><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:%s"/></Data>\r\n'%URN_UUID_ATTACHMENT
request += '</fileAttachment>\r\n'
request += '</uploadFileRequest>\r\n'
return request
def build_mime_message( request, data ):
'''
Build the xml string with MIME attachments and the base64 encoded data string
'''
request_part = '\r\n'
request_part += '--%s\r\n' % MIME_BOUNDARY
request_part += 'Content-Type: application/xop+xml; charset=UTF-8; type="text/xml; charset=UTF-8"\r\n'
request_part += 'Content-Transfer_Encoding: binary\r\n'
request_part += 'Content-ID: %s\r\n\r\n' % URN_UUID_REQUEST
request_part += '%s\r\n' % request
binary_part = '\r\n'
binary_part += '--%s\r\n' % MIME_BOUNDARY
binary_part += 'Content-Type: application/octet-stream\r\n'
binary_part += 'Content-Transfer-Encoding: base64\r\n'
binary_part += 'Content-ID: <%s>\r\n\r\n' % URN_UUID_ATTACHMENT
binary_part += '%s\r\n' % data
binary_part += '--%s--' % MIME_BOUNDARY
return request_part + binary_part
request = build_request( request_dict )
request = build_mime_message( request, data )#data is base64 encoded gzip compressed xml file
标题看起来像这样:
content_type_string = 'multipart/related;'
content_type_string += ' boundary=%s;' % MIME_BOUNDARY
content_type_string += ' type="application/xop+xml";'
content_type_string += ' start="%s";' % URN_UUID_REQUEST
content_type_string += ' start-info="text/xml"'
headers = {
'X-EBAY-SOA-OPERATION-NAME': 'uploadFile',
'X-EBAY-SOA-SERVICE-NAME': 'FileTransferService',
'X-EBAY-SOA-SECURITY-TOKEN': #auth token,
'Content-Length': len( request ),
'Content-Type': content_type_string
}
所以从这一切开始,我收集到的问题是标题和请求中的换行符和制表符。