eBay Trading API - calling structure in Delphi

2019-08-27 18:48发布

I am trying to call eBay API through Delphi. After long time searching through Google and StackOverflow, I can get "GeteBayTime" (Shopping API) and "findItemsByKeywords" (Finding API) work properly now.

When I started to test with Trading API, I have no idea where and how to insert seller's account info (userid and password). My first program for trading API is shown below and it is started with API call "GetSellingManagerSoldListings".

Can any one show me some clue to make Trading API work? And if anything else needs to be added to HTTP.header or Paramters?

Thanks.

procedure TForm1.btnEBayGetSoldListClick(Sender: TObject);
var
  sURL, sResponse, sEndpoint: String;
  jResult: TJSONObject;
  sCallName, sSiteID, sVersion: String;
  k: Integer;
  sParameters: String;
  sHeaders: TStringList;
  sRequestBody: TStringStream;
begin
  // Trading API
  sEndpoint := 'https://api.sandbox.ebay.com/ws/api.dll';

  sCallName := 'GetSellingManagerSoldListingsRequest';
  sSiteID := '15';
  sVersion := '967';
  sAppID := 'myAppID';
  sDevID := 'myDevID';
  sToken : 'myToken';    // OAuth Token?

  sURL := sEndpoint
       + '?callname=' + sCallName
       + '&siteid=' + '15';

  sHeaders := TStringList.Create;
  with sHeaders do begin
    Add('X-EBAY-API-COMPATIBILITY-LEVEL' + '=' + sVersion);
    Add('X-EBAY-API-DEV-NAME' + '=' + sDevID);
    Add('X-EBAY-API-APP-NAME' + '=' + sAppID);
    Add('X-EBAY-API-CERT-NAME' + '=' + sCertID);
    Add('X-EBAY-API-CALL-NAME' + '=' + sCallName);
    Add('X-EBAY-API-SITEID' + '=' + sSiteID);
  end;

  objHTTP.Request.ContentType := 'text/xml';

  with objHTTP.Request.CustomHeaders do begin
    Clear;
    AddStdValues(sHeaders);
  end;
  sHeaders.Free;

  sParameters := '<GetSellingManagerSoldListingsRequest xmlns="urn:ebay:apis:eBLBaseComponents">'
               + '  <RequesterCredentials>'
               + '    <eBayAuthToken>' + sToken + '</eBayAuthToken>'
               + '   <Filter>' + 'PaidNotShipped' + '</Filter>'
               + '  </RequesterCredentials>'
               + '</GetItemTransactionsRequest>';
  sRequestBody := TStringStream.Create(sParameters, TEncoding.UTF8);

  try
    sResponse := objHTTP.Post(sURL, sRequestBody);
    memHTML.Lines.Add(sResponse);
    memHTML.Lines.Add('');
  except
    sResponse := objHTTP.ResponseText;
    memHTML.Lines.Add(sResponse);
    memHTML.Lines.Add('');
  end;
end;

1条回答
贼婆χ
2楼-- · 2019-08-27 19:21

Here You go: Complete working GetSellingManagerSoldListingsRequest SOAP envelope:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
    <NS1:RequesterCredentials xmlns:NS1="urn:ebay:apis:eBLBaseComponents">
        <eBayAuthToken xmlns="urn:ebay:apis:eBLBaseComponents">EbayToken</eBayAuthToken>
        <NS1:Credentials>
            <AppId xmlns="urn:ebay:apis:eBLBaseComponents">xxx</AppId>
            <DevId xmlns="urn:ebay:apis:eBLBaseComponents">xxx</DevId>
            <AuthCert xmlns="urn:ebay:apis:eBLBaseComponents">xxx</AuthCert>
        </NS1:Credentials>
    </NS1:RequesterCredentials>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
    <GetSellingManagerSoldListingsRequest xmlns="urn:ebay:apis:eBLBaseComponents">
        <DetailLevel>ReturnAll</DetailLevel>
        <ErrorLanguage>en_GB</ErrorLanguage>
        <Version>945</Version>
        <Search>
            <SearchType>SaleRecordID</SearchType>
            <SearchValue>xxx</SearchValue>
        </Search>
        <Archived>false</Archived>
        <SaleDateRange>
            <TimeFrom>2018-08-12T17:59:32.939+02:00</TimeFrom>
            <TimeTo>2018-11-05T14:59:32.940+01:00</TimeTo>
        </SaleDateRange>
    </GetSellingManagerSoldListingsRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You may omit "search" tags to get full listing. Most of the trading api requests work this way.

Simplest way to do it in delphi:

Just put THTTPReqResp from WebServices tab on the form instead of TidHTTP, set url (THTTPReqResp1.URL) and run with THTTPReqResp1.Execute(const DataMsg: String; Resp: TStream); where DataMsg is provided SOAP envelope and response is saved to TStream (e.g TStringStream). You may also need to set InvokeOptions -> soIgnoreInvalidCerts in some cases.

Although it is possible to "assemble" request the way You did, i'd recomend to use WSDL wizard, import EBAY WSDL use WSDL Pruner Tool to cut less important functions (to avoid "E2491 Maximum VIRDEF count exceeded; check for recursion") and proceed with auto-generated unit.

查看更多
登录 后发表回答