如何使用Google Apps脚本通过USPS API获得国内运费? [英] How do I get domestic shipping rates via the USPS API using Google Apps Script?

查看:68
本文介绍了如何使用Google Apps脚本通过USPS API获得国内运费?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的浏览器中加载此链接后,该链接由USPS提供的用户ID和有关软件包的详细信息组成

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>

但是,当尝试使用以下代码块从Google Apps脚本编辑器中的函数加载API时:

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

这是我返回的错误,

<?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参数上.尝试以下代码:

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

查看文档 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆