< Day Day Up > |
Use the eBay API to search for PayPal-enabled listings . eBay and PayPal are a natural fit. eBay buyers love to pay with PayPal because it's quick and easy, so the vast majority of items listed for sale on eBay accept PayPal. This hack uses the eBay API to search for listings at http://www.ebay.com that accept PayPal.
To create a search application with the eBay SDK, you must first perform a few preliminary setup steps:
8.14.1 The CodeNow that you've done the preparatory work, it's time to write your application. Create a Windows forms application with a small text box called txtSearch , a button called btnSearch , and a listbox, lstItem , in which to store the search output. To call the functions in the SDK, begin by making a reference to the assembly eBay.SDK.dll from your project in Visual Studio .NET. Then, insert the appropriate include files at the top of your form's code window: using eBay.SDK; using eBay.SDK.API; using eBay.SDK.Model; using eBay.SDK.Model.Item; Finally, create a Click event handler for the button that performs the search and displays the results: private void btnSearch_Click(object sender, System.EventArgs e) { IItemFoundCollection items; GetSearchResultsCall search = new GetSearchResultsCall(CreateSession( )); search.Query = txtSearch.Text; search.PayPalItemsOnly = true; search.MaxResults = 20; // can be up to 200; more if you use paging items = search.GetSearchResults( ); foreach(IItem it in items) { lstItem.Items.Add(it.Title); } } Because the majority of the communication and data-handling code is wrapped by the classes provided by the SDK, the code you have to write is fairly straightforward. To do the search, this procedure simply creates an instance of the GetSearchResultsCall object, assigns values to its properties, and then calls the object's GetSearchResults method.
The return value of GetSearchResults is a typed IItemFoundCollection that is populated with IItem objects, each of which represents an item listed for sale on eBay. After the function returns the collection of eBay items, the foreach loop uses it to populate the listbox with their titles. There's one part of this code that can be a little tricky: creating a session object. The eBay ApiSession object is required to be passed to the server along with every eBay API call. Our event handler gets an ApiSession object by calling a function called CreateSession , which looks like this: private ApiSession CreateSession( ) { ApiSession sess = new eBay.SDK.API.ApiSession( ); sess.Developer = ConfigurationSettings.AppSettings["DeveloperID"]; sess.Certificate = ConfigurationSettings.AppSettings["Certificate"]; sess.Application = ConfigurationSettings.AppSettings["ApplicationID"]; IApiToken t = new ApiToken( ); t.Token = ConfigurationSettings.AppSettings["Token"]; sess.Token = t; sess.Url = ConfigurationSettings.AppSettings["ServerUrl"]; return sess; } 8.14.2 Running the HackThis code expects to find the configuration information it needs in a .NET XML configuration file, called Web.config if you're writing a web application or ExeNam e.config (where ExeName is the name of the executable) if you're creating a compiled binary application. A typical configuration file of an eBay application written in any .NET language looks like this: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DeveloperID" value=" mydevid " /> <add key="ApplicationID" value=" myappid " /> <add key="Certificate" value=" mycert " /> <add key="ServerUrl" value="https://api.sandbox.ebay.com/ws/api.dll" /> <add key="Token" value=" AgAAAA**AQAAAA**aAAAAA**n8yAQA " /> </appSettings> </configuration> For this to work, you need to replace the italicized values in the appSettings section ( mydevid , myappid , and mycert ) with your developer keys (sent to you from eBay after registering in step 1, earlier in this hack) and your security token (generated in step 4). Finally, the ServerUrl value provided here is the correct URL for the eBay development Sandbox. (You'll use a different URL to take the application live.) Compile your application, and give it a whirl! 8.14.3 Hacking the HackThere are many more things you can do with the eBay API besides search. One of the most common operations involves automatically listing items for sale, typically to save time in the selling process or provide integration between your inventory database and eBay. You can also use the eBay API to obtain details about listings in progress, download high-bidder information for completed items, and even create notifications when a bidder with negative feedback bids on one of your auctions! There are more than 70 calls in the eBay API, and the SDK provides quite a few code examples in a number of different programming languages. Jeffrey McManus |
< Day Day Up > |