问题
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<sup>™</sup></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