Google Apps 脚本 USPS 地址验证 API
Google Apps Script USPS Address Validation API
我正在尝试从 Google Apps 脚本中调用 USPS 地址验证 API。它需要 XML,与 JSON 相比,我没有经验。这是另一个类似的 Whosebug 问题 (How do I get domestic shipping rates via the USPS API using Google Apps Script?),我已经按照它找到了 "t",但它已经足够不同了,当适用于 USPS 地址验证 API 时,它是不适合我。我已经注册了一个用户 ID 并收到了 USPS 的确认。
这是 USPS 地址验证 API 文档:https://www.usps.com/business/web-tools-apis/address-information-api.htm#_Toc34052588。
这是我的 Apps 脚本代码(出于保密原因更改了 USPS 用户 ID):
var site = 'https://secure.shippingapis.com/ShippingAPI.dll';
var userid = "999ABCDE9999";
var payload = {
"API": "Verify",
"XML" : "<AddressValidateRequest USERID=\"" + userid + "\"> \
<Revision>0</Revision> \
<Address ID=\"0\"> \
<Address2>" + address1 + "</Address2> \
<City>" + city1 + "</City> \
<State>" + state1 + "</State> \
<Zip5>" + zip1 + "</Zip5> \
</Address> \
</AddressValidationRequest>"
};
var options = {
method: "POST",
payload: payload
}
Logger.log('payload = ' + JSON.stringify(payload));
var response = UrlFetchApp.fetch(site, options);
var xmlreturn = response.getContentText();
Logger.log('return xml = ' + xmlreturn);
这是我的日志(街道地址也为保密而更改:
payload = {"API":"Verify","XML":"<AddressValidateRequest USERID=\"999ABCDE9999\"> <Revision>0</Revision> <Address ID=\"0\"> <Address2>123 AnyStreet Dr</Address2> <City>Forney</City> <State>TX</State> <Zip5>75126</Zip5> </Address> </AddressValidationRequest>"}
return xml = <?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.(B)</Description><Source>USPSCOM::DoAuth</Source></Error>
这是 return/result 无论我尝试做什么,我都会得到。我想,也许,一个问题可能是 payload.XML 包括 USERID=\"999ABCDE9999\" 而不是文档所说的 - USERID="999ABCDE9999" (没有反斜杠),但我不知道如何摆脱那些(并尝试过)。
感谢您的帮助!
我明白问题出在哪里了。我唯一真正的问题是我的结束 XML 标签不正确。它应该是 < /AddressValidateRequest > 而不是 < /AddressValidationRequest >。您还需要在 之前和 < Zip4/ > 之后包括一个自闭合的 标记,在 < /Zip5 > 之后。如果你不这样做,它会抛出一个错误。
但是,我很高兴我遇到了问题并将其发布在这里,因此希望这会在未来帮助其他人获得代码,就像您从 Google 应用程序脚本。我在其他任何地方都没有找到这个。这是最终代码,然后我将在下面谈谈我是如何处理结果的。
var userid = 'Enter the ID USPS Gave you when you signed up here';
var url = 'https://secure.shippingapis.com/ShippingAPI.dll';
var payload = {
"API": "Verify",
"XML" : "<AddressValidateRequest USERID=\"" + userid + "\"> \
<Revision>1</Revision> \
<Address ID=\"0\"> \
<Address1/> \
<Address2>" + address1 + "</Address2> \
<City>" + city1 + "</City> \
<State>" + state1 + "</State> \
<Zip5>" + zip1 + "</Zip5> \
<Zip4/> \
</Address> \
</AddressValidateRequest>"
};
var options = {
"method": "POST",
"payload": payload,
muteHttpExceptions: true
}
// Use a try & catch routine to catch any major errors.
try {
var response = UrlFetchApp.fetch(url, options);
}
catch (e) {
Logger.log(e);
continue; // If there is an error (i.e. the API service is not available), then skip the rest of the logic for this row.
}
var xmlReturn = response.getContentText();
从提取中获得 "xmlreturn"(正如我上面所说的)后,您需要解析 XML 响应以获取从 USPS 返回的每个地址。这是我如何使用正则表达式为地址行 2 做的。你对其他部分做同样的事情,只是稍微改变你的正则表达式。
// *********************************
// Get Address Line 2
// *********************************
var uspsAddress2Reg = /(?<=<Address2>)[^]+(?=<\/Address2>)/g;
var uspsAddress2Arr =xmlReturn.match(uspsAddress2Reg);
// If we found the Address Line 2 in the following format: <Address2>1234 ANYSTREET DRIVE</Address2>
if (uspsAddress2Arr != null) {
var uspsAddress2 = uspsAddress2Arr[0];
} else {
var uspsAddress2 = ''; // If not found, set it to blank.
} // End If (uspsAddress2Arr != null)
我正在尝试从 Google Apps 脚本中调用 USPS 地址验证 API。它需要 XML,与 JSON 相比,我没有经验。这是另一个类似的 Whosebug 问题 (How do I get domestic shipping rates via the USPS API using Google Apps Script?),我已经按照它找到了 "t",但它已经足够不同了,当适用于 USPS 地址验证 API 时,它是不适合我。我已经注册了一个用户 ID 并收到了 USPS 的确认。
这是 USPS 地址验证 API 文档:https://www.usps.com/business/web-tools-apis/address-information-api.htm#_Toc34052588。
这是我的 Apps 脚本代码(出于保密原因更改了 USPS 用户 ID):
var site = 'https://secure.shippingapis.com/ShippingAPI.dll';
var userid = "999ABCDE9999";
var payload = {
"API": "Verify",
"XML" : "<AddressValidateRequest USERID=\"" + userid + "\"> \
<Revision>0</Revision> \
<Address ID=\"0\"> \
<Address2>" + address1 + "</Address2> \
<City>" + city1 + "</City> \
<State>" + state1 + "</State> \
<Zip5>" + zip1 + "</Zip5> \
</Address> \
</AddressValidationRequest>"
};
var options = {
method: "POST",
payload: payload
}
Logger.log('payload = ' + JSON.stringify(payload));
var response = UrlFetchApp.fetch(site, options);
var xmlreturn = response.getContentText();
Logger.log('return xml = ' + xmlreturn);
这是我的日志(街道地址也为保密而更改:
payload = {"API":"Verify","XML":"<AddressValidateRequest USERID=\"999ABCDE9999\"> <Revision>0</Revision> <Address ID=\"0\"> <Address2>123 AnyStreet Dr</Address2> <City>Forney</City> <State>TX</State> <Zip5>75126</Zip5> </Address> </AddressValidationRequest>"}
return xml = <?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.(B)</Description><Source>USPSCOM::DoAuth</Source></Error>
这是 return/result 无论我尝试做什么,我都会得到。我想,也许,一个问题可能是 payload.XML 包括 USERID=\"999ABCDE9999\" 而不是文档所说的 - USERID="999ABCDE9999" (没有反斜杠),但我不知道如何摆脱那些(并尝试过)。
感谢您的帮助!
我明白问题出在哪里了。我唯一真正的问题是我的结束 XML 标签不正确。它应该是 < /AddressValidateRequest > 而不是 < /AddressValidationRequest >。您还需要在
但是,我很高兴我遇到了问题并将其发布在这里,因此希望这会在未来帮助其他人获得代码,就像您从 Google 应用程序脚本。我在其他任何地方都没有找到这个。这是最终代码,然后我将在下面谈谈我是如何处理结果的。
var userid = 'Enter the ID USPS Gave you when you signed up here';
var url = 'https://secure.shippingapis.com/ShippingAPI.dll';
var payload = {
"API": "Verify",
"XML" : "<AddressValidateRequest USERID=\"" + userid + "\"> \
<Revision>1</Revision> \
<Address ID=\"0\"> \
<Address1/> \
<Address2>" + address1 + "</Address2> \
<City>" + city1 + "</City> \
<State>" + state1 + "</State> \
<Zip5>" + zip1 + "</Zip5> \
<Zip4/> \
</Address> \
</AddressValidateRequest>"
};
var options = {
"method": "POST",
"payload": payload,
muteHttpExceptions: true
}
// Use a try & catch routine to catch any major errors.
try {
var response = UrlFetchApp.fetch(url, options);
}
catch (e) {
Logger.log(e);
continue; // If there is an error (i.e. the API service is not available), then skip the rest of the logic for this row.
}
var xmlReturn = response.getContentText();
从提取中获得 "xmlreturn"(正如我上面所说的)后,您需要解析 XML 响应以获取从 USPS 返回的每个地址。这是我如何使用正则表达式为地址行 2 做的。你对其他部分做同样的事情,只是稍微改变你的正则表达式。
// *********************************
// Get Address Line 2
// *********************************
var uspsAddress2Reg = /(?<=<Address2>)[^]+(?=<\/Address2>)/g;
var uspsAddress2Arr =xmlReturn.match(uspsAddress2Reg);
// If we found the Address Line 2 in the following format: <Address2>1234 ANYSTREET DRIVE</Address2>
if (uspsAddress2Arr != null) {
var uspsAddress2 = uspsAddress2Arr[0];
} else {
var uspsAddress2 = ''; // If not found, set it to blank.
} // End If (uspsAddress2Arr != null)