通过向 Ebay 发布交易 API 调用请求仅获取响应 [200]

发布于 2025-01-14 23:11:24 字数 3666 浏览 2 评论 0原文

我可以通过使用 Python SDK(使用 JSON 代码)或通过 https://developer 上的 API 浏览器发布 Trading API AddItem 调用.ebay.com/(使用与下面的 Python 请求库一起使用的同一 XML 文件中的相同 XML 代码)并像往常一样获得一些响应。

但是,当我尝试通过使用 xml 文件调用 Python requests 库来执行此操作时,我没有得到任何响应,而是得到了。有人可以看一下我下面的代码,看看如何修复它并从 Ebay 获得正常响应吗?

代码:


import requests

# Set the name of the XML file.
xml_file = XML_FILE

headers = {'Content-Type':'text/xml', "X-EBAY-API-SITEID": "0",
"X-EBAY-API-COMPATIBILITY-LEVEL": "967",
"X-EBAY-API-CALL-NAME":"AddItem"}

# Open the XML file.
with open(xml_file) as xml:
    r = requests.post('https://api.ebay.com/ws/api.dll', data=xml, headers=headers)

print(r)

我的 XML 文件:


<?xml version="1.0" encoding="utf-8"?>
<AddItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <RequesterCredentials>
    <eBayAuthToken>TOKEN</eBayAuthToken>
  </RequesterCredentials>
   <ErrorLanguage>en_US</ErrorLanguage>
   <WarningLevel>High</WarningLevel>
  <Item>
      <Title>Harry Potter and the Philosopher's Stone</Title>
      <Description>
        This is the first book in the Harry Potter series. In excellent condition!
      </Description>
      <PrimaryCategory>
        <CategoryID>29223</CategoryID>
      </PrimaryCategory>
      <StartPrice>1.0</StartPrice>
      <CategoryMappingAllowed>true</CategoryMappingAllowed>
      <Country>US</Country>
      <Currency>USD</Currency>
      <PaymentMethods>CreditCard</PaymentMethods>
      <DispatchTimeMax>3</DispatchTimeMax>
      <ListingDuration>Days_7</ListingDuration>
      <ListingType>Chinese</ListingType>
      <PictureDetails>
        <PictureURL>https://mysamplepicture.com/14.jpg</PictureURL>
      </PictureDetails>
      <PostalCode>95125</PostalCode>
      <Quantity>1</Quantity>
      <ItemSpecifics>
         <NameValueList>
            <Name>Title</Name>
            <Value>Harry Potter and the Philosophers Stone</Value>
         </NameValueList>
         <NameValueList>
            <Name>Publisher</Name>
            <Value>Smashwords</Value>
         </NameValueList>
         <NameValueList>
            <Name>Author</Name>
            <Value>JK Rowling</Value>
         </NameValueList>
         <NameValueList>
            <Name>Language</Name>
            <Value>English</Value>
         </NameValueList>
      </ItemSpecifics>
      <ReturnPolicy>
        <ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>
        <RefundOption>MoneyBack</RefundOption>
        <ReturnsWithinOption>Days_30</ReturnsWithinOption>
        <ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>
      </ReturnPolicy>
      <ShippingDetails>
        <ShippingType>Flat</ShippingType>
        <ShippingServiceOptions>
          <ShippingServicePriority>1</ShippingServicePriority>
          <ShippingService>USPSMedia</ShippingService>
          <ShippingServiceCost>2.50</ShippingServiceCost>
        </ShippingServiceOptions>
      </ShippingDetails>
      <Site>US</Site>
  </Item>
</AddItemRequest>

I can post Trading API AddItem call through using Python SDK (using JSON code) or through the API explorer on https://developer.ebay.com/ (using the same XML codes from the same XML file I used with the Python requests library below) and gets some response as usual.

However, when I tried to do so by calling through Python requests library with an xml file, I got no response and instead got <Response [200]>. Could someone pls take a look at my code below and see how I can fix it and get the normal response from Ebay?

Code:


import requests

# Set the name of the XML file.
xml_file = XML_FILE

headers = {'Content-Type':'text/xml', "X-EBAY-API-SITEID": "0",
"X-EBAY-API-COMPATIBILITY-LEVEL": "967",
"X-EBAY-API-CALL-NAME":"AddItem"}

# Open the XML file.
with open(xml_file) as xml:
    r = requests.post('https://api.ebay.com/ws/api.dll', data=xml, headers=headers)

print(r)

My XML file:


<?xml version="1.0" encoding="utf-8"?>
<AddItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
  <RequesterCredentials>
    <eBayAuthToken>TOKEN</eBayAuthToken>
  </RequesterCredentials>
   <ErrorLanguage>en_US</ErrorLanguage>
   <WarningLevel>High</WarningLevel>
  <Item>
      <Title>Harry Potter and the Philosopher's Stone</Title>
      <Description>
        This is the first book in the Harry Potter series. In excellent condition!
      </Description>
      <PrimaryCategory>
        <CategoryID>29223</CategoryID>
      </PrimaryCategory>
      <StartPrice>1.0</StartPrice>
      <CategoryMappingAllowed>true</CategoryMappingAllowed>
      <Country>US</Country>
      <Currency>USD</Currency>
      <PaymentMethods>CreditCard</PaymentMethods>
      <DispatchTimeMax>3</DispatchTimeMax>
      <ListingDuration>Days_7</ListingDuration>
      <ListingType>Chinese</ListingType>
      <PictureDetails>
        <PictureURL>https://mysamplepicture.com/14.jpg</PictureURL>
      </PictureDetails>
      <PostalCode>95125</PostalCode>
      <Quantity>1</Quantity>
      <ItemSpecifics>
         <NameValueList>
            <Name>Title</Name>
            <Value>Harry Potter and the Philosophers Stone</Value>
         </NameValueList>
         <NameValueList>
            <Name>Publisher</Name>
            <Value>Smashwords</Value>
         </NameValueList>
         <NameValueList>
            <Name>Author</Name>
            <Value>JK Rowling</Value>
         </NameValueList>
         <NameValueList>
            <Name>Language</Name>
            <Value>English</Value>
         </NameValueList>
      </ItemSpecifics>
      <ReturnPolicy>
        <ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>
        <RefundOption>MoneyBack</RefundOption>
        <ReturnsWithinOption>Days_30</ReturnsWithinOption>
        <ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>
      </ReturnPolicy>
      <ShippingDetails>
        <ShippingType>Flat</ShippingType>
        <ShippingServiceOptions>
          <ShippingServicePriority>1</ShippingServicePriority>
          <ShippingService>USPSMedia</ShippingService>
          <ShippingServiceCost>2.50</ShippingServiceCost>
        </ShippingServiceOptions>
      </ShippingDetails>
      <Site>US</Site>
  </Item>
</AddItemRequest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2025-01-21 23:11:24

您似乎对 requests 模块非常陌生。只需阅读 docs ,hello world 示例将解决您的问题。

我们经常使用 r.text 表示文本,r.content 表示二进制,r.json 表示 json。 r.status_coder.reason 用于异常调试。

You seem to be very new to requests module. Just read the docs and the hello world example will solve your problem.

We often use r.text for text, r.content for binary, r.json for json. r.status_code and r.reason for exception debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文