簡體   English   中英

如何使用Google Apps腳本通過USPS API獲取國內運費?

[英]How do I get domestic shipping rates via the USPS API using Google Apps Script?

在我的瀏覽器中加載此鏈接,該鏈接由USPS提供的用戶ID和有關包的詳細信息組成

Link: http://production.shippingapis.com/ShippingAPITest.dll?API=RateV4&XML=<RateV4Request USERID="[userid]"><Revision/><Package ID="1ST"><Service>PRIORITY</Service><ZipOrigination>02211</ZipOrigination><ZipDestination>90210</ZipDestination><Pounds>5</Pounds><Ounces>2</Ounces><Container>RECTANGULAR</Container><Size>LARGE</Size><Width>15</Width><Length>30</Length><Height>15</Height><Girth>55</Girth></Package></RateV4Request>

我返回正確的結果(如下)

<RateV4Response>
  <Package ID="1ST">
    <ZipOrigination>02211</ZipOrigination>
    <ZipDestination>90210</ZipDestination>
    <Pounds>5</Pounds>
    <Ounces>2</Ounces>
    <Container>RECTANGULAR</Container>
    <Size>LARGE</Size>
    <Width>15</Width>
    <Length>30</Length>
    <Height>15</Height>
    <Zone>8</Zone>
    <Postage CLASSID="1">
      <MailService>Priority Mail 2-Day&lt;sup&gt;&#8482;&lt;/sup&gt;</MailService>
      <Rate>86.65</Rate>
    </Postage>
  </Package>
</RateV4Response>

但是,嘗試使用以下代碼塊從Google Apps腳本編輯器中的函數加載API時:

function xmlLoader(){
  var pounds = 5;
  var ounces = 2;
  var userid = "[userid]";
  var url = "http://production.shippingapis.com/ShippingAPI.dll";
  var options =
    {
      "API" : "RateV4",
      "XML" : "<RateV4Request USERID=\"" + userid + "\"> \
                 <Revision/> \
                 <Package ID=\"1ST\"> \
                   <Service>PRIORITY</Service> \
                   <ZipOrigination>02211</ZipOrigination> \
                   <ZipDestination>90210</ZipDestination> \
                   <Pounds>" + pounds + "</Pounds> \
                   <Ounces>" + ounces + "</Ounces> \
                   <Container>RECTANGULAR</Container> \
                   <Size>LARGE</Size> \
                   <Width>15</Width> \
                   <Length>30</Length> \
                   <Height>15</Height> \
                   <Girth>55</Girth> \
                 </Package> \
               </RateV4Request>"
    };

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
};

這是我回復的錯誤,

<?xml version="1.0" encoding="UTF-8"?>
  <Error>
    <Number>80040B19</Number>
    <Description>XML Syntax Error: Please check the XML request to see if it can be parsed.</Description>
    <Source>USPSCOM::DoAuth</Source>
  </Error>

任何幫助將不勝感激

我沒有任何usps ID,所以我不能進行全面測試,但對我來說問題來自你給出的URLFetch參數。 試試這段代碼:

function xmlLoader(){
  var pounds = 5;
  var ounces = 2;
  var userid = "1000"; //"[userid]";
  var url = "http://production.shippingapis.com/ShippingAPI.dll";
  var payload =
    {
      "API" : "RateV4",
      "XML" : "<RateV4Request USERID=\"" + userid + "\"> \
                 <Revision/> \
                 <Package ID=\"1ST\"> \
                   <Service>PRIORITY</Service> \
                   <ZipOrigination>02211</ZipOrigination> \
                   <ZipDestination>90210</ZipDestination> \
                   <Pounds>" + pounds + "</Pounds> \
                   <Ounces>" + ounces + "</Ounces> \
                   <Container>RECTANGULAR</Container> \
                   <Size>LARGE</Size> \
                   <Width>15</Width> \
                   <Length>30</Length> \
                   <Height>15</Height> \
                   <Girth>55</Girth> \
                 </Package> \
               </RateV4Request>"
    };

  var options={
    method:"POST",
    payload:payload
  }

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
};

看看這里的文檔

另請注意,USPS和UPS都沒有最好的XML解釋器。 我發現這篇文章正在搜索解析公司名稱中&符號的HTML實體的語法問題。 (應該支持XML,但顯然它會導致USPS嘔吐)其中一些人對“訂單”也非常挑剔。 如同未完全按照文檔指定的那樣,元素順序將導致錯誤。 我也遇到了他們聲稱'是可選的但不是 - 我需要發送一個'空'標簽的字段的問題 - 以及其他那些用空標簽打印的問題。 我昨天遇到的另一件事,驗證地址api似乎想要翻轉address1和address2,但僅限於輸出。 (在輸入時,如果你只發送地址2就會嘔吐)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM