eBay Trading API - calling structure in Delphi

感情迁移 提交于 2020-01-06 05:31:07

问题


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:


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.



来源:https://stackoverflow.com/questions/53154983/ebay-trading-api-calling-structure-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!