1

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 Script エディタの関数から 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>

どんな援助でも大歓迎です

4

2 に答える 2

2

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

于 2014-03-07T08:33:25.260 に答える
1

また、USPS と UPS の両方に優れた XML インタープリターがないことにも注意してください。会社名に含まれるアンパサンドの HTML エンティティを解析する構文の問題を検索するこの記事を見つけました。(これは XML でサポートされているはずですが、どうやら USPS の吐き気の原因になっているようです) それらのいくつかは、'注文' についても非常にうるさいです。同様に、要素の順序は、ドキュメントで指定されているとおりに正確に従わないと、エラーが発生します。また、「主張」しているフィールドで問題が発生しましたが、そうではありません-「空の」タグを送信する必要がありました-および空のタグを突いた他のフィールド。昨日私が遭遇したもう 1 つのことは、検証アドレス API が address1 と address2 を入れ替えるのが好きなようですが、出力のみです。(入力時に、address2 だけを送信すると吐き出します)。

于 2014-05-28T15:14:50.203 に答える