如何使用Google Apps脚本成功将XML请求发送到eBay的API服务器?

时间:2014-03-10 19:57:41

标签: javascript xml api google-apps-script ebay

我正确使用的功能会返回一条消息,说明" API调用" GeteBayOfficialTime"在此版本中无效或不受支持。"

function GetTime() {
    var site = "https://api.ebay.com/ws/api.dll";
    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \
           <GeteBayOfficialTimeRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\"> \
             <RequesterCredentials> \
               <eBayAuthToken>*******</eBayAuthToken> \
             </RequesterCredentials> \
           </GeteBayOfficialTimeRequest>";
    var payload =
      {
        "Content-Type": "text/xml",
        "X-EBAY-API-SITEID": "0",
        "X-EBAY-API-COMPATIBILITY-LEVEL": "759",
        "X-EBAY-API-CALL-NAME": "GeteBayOfficialTime",
        "XML": xml
      };

  var options = 
    {
      method:"POST",
      payload:payload
    };
  var response = UrlFetchApp.fetch(site, options);

  var xml = response.getContentText();
};

经过一些搜索后,我发现此问题的主要原因是标题不正确,但是,我不确定如何设置它们除了当前实现的内容之外的其他方法。

1 个答案:

答案 0 :(得分:1)

您将标头和xml都传入请求的有效负载。标题应该通过自己的字段传入。下面更新的代码应该适合您。

function GetTime() {
    var site = 'https://api.ebay.com/ws/api.dll';

    var xml = '<?xml version="1.0" encoding="utf-"?> \
        <GeteBayOfficialTimeRequest xmlns="urn:ebay:apis:eBLBaseComponents"> \
            <RequesterCredentials> \
                <eBayAuthToken>*********</eBayAuthToken> \
            </RequesterCredentials> \
        </GeteBayOfficialTimeRequest>';

    var headers = {
        'Content-Type': 'text/xml',
        'X-EBAY-API-SITEID': '0',
        'X-EBAY-API-COMPATIBILITY-LEVEL': '861',
        'X-EBAY-API-CALL-NAME': 'GeteBayOfficialTime'
    };

    var options = {
        method: 'POST',
        headers: headers,
        payload: xml
    };

    var response = UrlFetchApp.fetch(site, options);

    var xml = response.getContentText();
};