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

和自甴很熟 提交于 2021-02-08 08:12:23

问题


Upon loading this link, which is composed of the user ID provided by USPS and details regarding the package, in my browser

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>

I am returned with the correct result (below)

<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>

However, when trying to load the API from a function in Google Apps Script editor using the following block of code:

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());
};

This is the error I am returned with,

<?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>

Any assistance would be greatly appreciated


回答1:


I don't have any usps ID so I can't do the full test, but for me the problem come from the the URLFetch arguments you are giving. try this code:

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());
};

have a look at the doc here




回答2:


Also keep in mind that both USPS and UPS don't have the greatest of XML interpreters. I found this article searching for a syntax problem parsing an HTML entity for an ampersand in a company name. (which should be supported in XML but apparently it causes USPS to puke) Some of them are also very picky on the 'order'. As in, the element order will cause errors if not followed exactly as their documentation specifies. I have also run into issues on fields they 'claim' are optional but that aren't - I needed to send an 'empty' tag - and others that puked on empty tags. Still another thing I ran into yesterday, the verify address api seems to like to flip address1 and address2 around, but only on output. (on input it pukes if you send just address2).



来源:https://stackoverflow.com/questions/22239961/how-do-i-get-domestic-shipping-rates-via-the-usps-api-using-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!