我正在尝试使用他们的FileTransfer API向eBay上传批量数据交换呼叫。为了做到这一点,我必须POST一个xml请求字符串到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}>rn
{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""
}
然后是my 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?
谢谢你的帮助!
韦斯
我知道我的请求哪里出了问题。它是构造请求字符串和头的组合。我采用了一种更有组织、更程序化的方法,结果奏效了。
下面是构建请求字符串的部分:###########################################
# 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">rn'
request += '<taskReferenceId>%s</taskReferenceId>rn' % request_dict['taskReferenceId']
request += '<fileReferenceId>%s</fileReferenceId>rn' % request_dict['fileReferenceId']
request += '<fileFormat>%s</fileFormat>rn' % request_dict['fileFormat']
request += '<fileAttachment>rn'
request += '<Size>%s</Size>rn'% request_dict['Size']
request += '<Data><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:%s"/></Data>rn'%URN_UUID_ATTACHMENT
request += '</fileAttachment>rn'
request += '</uploadFileRequest>rn'
return request
def build_mime_message( request, data ):
'''
Build the xml string with MIME attachments and the base64 encoded data string
'''
request_part = 'rn'
request_part += '--%srn' % MIME_BOUNDARY
request_part += 'Content-Type: application/xop+xml; charset=UTF-8; type="text/xml; charset=UTF-8"rn'
request_part += 'Content-Transfer_Encoding: binaryrn'
request_part += 'Content-ID: %srnrn' % URN_UUID_REQUEST
request_part += '%srn' % request
binary_part = 'rn'
binary_part += '--%srn' % MIME_BOUNDARY
binary_part += 'Content-Type: application/octet-streamrn'
binary_part += 'Content-Transfer-Encoding: base64rn'
binary_part += 'Content-ID: <%s>rnrn' % URN_UUID_ATTACHMENT
binary_part += '%srn' % 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
}
所以从所有这些,我收集到的问题是与标题和请求中的换行符和制表符。