eBay大中华区API开发者平台 开发者专区

搜 索
首页>API开发者平台>技术文档>Console Add Item –C#.NEW 使用eBay API SDK刊登商品

技术文档

问题
Console Add Item –C#.NEW 使用eBay API SDK刊登商品
解答
0
人觉得答案有帮助)
本教程是一个简单的例子,教您如何使用eBay SDK/APIeBay 站点上登录一个新的商品。您可以在.Net SDK包的samples/C#/ConsoleAddItem文件夹中找到本教程中用到的项目代码。

项目运行的前提:

1. .Net2.0版本的eBay Trading SDK

2.一个eBay用户令牌(eBay user token)

3. Visual Studio 20052008

 

创建ConsoleAddItem例子的步骤:

1.Visual Studio中新建一个叫ConsoleAddItem的控制台应用程序(见图一)

图一  ConsoleAddItem项目

2. 将一个新的应用程序配置文件(App.config,见图二)添加到这个项目中。这个文件包含配置信息,例如调用eBay API所需的eBay 用户令牌(eBay user token)eBay Trading API服务器地址(见清单一)。在运行本例之前请使用您的令牌更新文件中的令牌值。

图二  文件App.config

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <!-- API Server URL, required -->

    <!-- For production site use: https://api.ebay.com/wsapi -->

    <!-- For Sandbox site use: https://api.sandbox.ebay.com/wsapi -->

    <add key="Environment.ApiServerUrl" value="https://api.sandbox.ebay.com/wsapi"/>

    <!-- User token for API server access, required -->

    <add key="UserAccount.ApiToken" value="you ebay user token"/>

  </appSettings>

</configuration>

清单一  App.config文件内容

3. eBay.Service类库和.Net System.configuration 类库的引用添加到项目中。eBay.Service类库可以在您安装的.NeteBay SDK的根目录下找到。eBay.Service程序集是SDK的主要组成部分。它封装了eBay API调用并对您隐藏了底层通信细节。

图三  eBay.Service程序集

4.在主程序类(见图四)中,导入下列.Net名字空间和SDK名字空间(见清单二),同时提供相应的名字空间声明,类名和变量定义。

图四  主程序

 

package consoleadditem;

using System;

using System.Configuration;

using eBay.Service.Call;

using eBay.Service.Core.Sdk;

using eBay.Service.Core.Soap;

using eBay.Service.Util;

 

namespace ConsoleAddItem

{

    class Program

    {

        private static ApiContext apiContext = null;

 

清单二  导入名字空间

5.为了调用eBay API,首先你需要初始化ApiContext对象。ApiContext对象包含了所有调用eBay API所需的配置信息。

在主程序类中,新建一个名为GetApiContext的方法(见清单三)。在这个方法中,我们通过获取用户在控制台中输入的用户令牌和服务器地址来初始化ApiContext对象。另外我们在ApiContext对象中设置站点为eBay US

您可以配置ApiContext对象把所有的通信记录到一个日志中(比如一个文本文件)。这里我们将在一个文本文档中记录所有的SOAP消息。一旦出错,日志对于排错将很有帮助。

        /// <summary>

        /// Populate eBay SDK ApiContext object with data from application configuration file

        /// </summary>

        /// <returns>ApiContext object</returns>

        static ApiContext GetApiContext()

        {

            //apiContext is a singleton

            //to avoid duplicate configuration reading

            if (apiContext != null)

            {

                return apiContext;

            }

            else

            {

                apiContext = new ApiContext();

 

                //set Api Server Url

                apiContext.SoapApiServerUrl =

                    ConfigurationManager.AppSettings["Environment.ApiServerUrl"];

                //set Api Token to access eBay Api Server

                ApiCredential apiCredential = new ApiCredential();

                apiCredential.eBayToken =

                    ConfigurationManager.AppSettings["UserAccount.ApiToken"];

                apiContext.ApiCredential = apiCredential;

                //set eBay Site target to US

                apiContext.Site = SiteCodeType.US;

 

                //set Api logging

                apiContext.ApiLogManager = new ApiLogManager();

                apiContext.ApiLogManager.ApiLoggerList.Add(

                    new FileLogger("listing_log.txt", true, true, true)

                    );

                apiContext.ApiLogManager.EnableLogging = true;

 

 

                return apiContext;

            }

        }

清单三  GetApiContext方法

6.为了在eBay站点上登录一个商品,首先您需要新建ItemType类的一个实例。

在主程序类中,新建一个BuildItem的方法(见清单四)。在这个方法中,我们通过具体商品的标题、描述、类别、支付方法和货运细节等信息来构建一个item。关于如何构造item的详细信息,请参考eBay Trading API documentation.

        /// <summary>

        /// Build a sample item

        /// </summary>

        /// <returns>ItemType object</returns>

        static ItemType BuildItem()

        {

            ItemType item = new ItemType();

 

            // item title

            item.Title = "Test Item";

            // item description

            item.Description = "eBay SDK sample test item";

 

            // listing type

            item.ListingType = ListingTypeCodeType.Chinese;

            // listing price

            item.Currency = CurrencyCodeType.USD;

            item.StartPrice = new AmountType();

            item.StartPrice.Value = 20;

            item.StartPrice.currencyID = CurrencyCodeType.USD;

 

            // listing duration

            item.ListingDuration = "Days_3";

 

            // item location and country

            item.Location = "San Jose";

            item.Country = CountryCodeType.US;

 

            // listing category, Photography Software

            CategoryType category = new CategoryType();

            category.CategoryID = "30022";

            item.PrimaryCategory = category;

 

            // item quality

            item.Quantity = 1;

 

            // payment methods

            item.PaymentMethods = new BuyerPaymentMethodCodeTypeCollection();

            item.PaymentMethods.AddRange(

                new BuyerPaymentMethodCodeType[] { BuyerPaymentMethodCodeType.PayPal }

                );

            // e*** is required if paypal is used as payment method

            item.PayPalE***Address = "me@ebay.com";

 

            // item condition, New

            item.ConditionID = 1000; 

 

            // handling time is required

            item.DispatchTimeMax = 1;

 

            // shipping details

            item.ShippingDetails = BuildShippingDetails();

 

            // return policy

            item.ReturnPolicy = new ReturnPolicyType();

            item.ReturnPolicy.ReturnsAcceptedOption = "ReturnsAccepted";

 

            return item;

        }

清单四  BuildItem方法

7.在主程序类中,创建一个名为BuildShippingDetails的工具方法(见清单五)。该方法用于构造货运细节,BuildItem方法会调用这个方法。

 

        /// <summary>

        /// Build sample shipping details

        /// </summary>

        /// <returns>ShippingDetailsType object</returns>

        static ShippingDetailsType BuildShippingDetails()

        {

            // Shipping details

            ShippingDetailsType sd = new ShippingDetailsType();

 

            sd.ApplyShippingDiscount = true;

            AmountType amount = new AmountType();

            amount.Value = 2.8;

            amount.currencyID = CurrencyCodeType.USD;

            sd.PaymentInstructions = "eBay .Net SDK test instruction.";

 

            // Shipping type and shipping service options

            sd.ShippingType = ShippingTypeCodeType.Flat;

            ShippingServiceOptionsType shippingOptions = new ShippingServiceOptionsType();

            shippingOptions.ShippingService =

                ShippingServiceCodeType.ShippingMethodStandard.ToString();

            amount = new AmountType();

            amount.Value = 2.0;

            amount.currencyID = CurrencyCodeType.USD;

            shippingOptions.ShippingServiceAdditionalCost = amount;

            amount = new AmountType();

            amount.Value = 10;

            amount.currencyID =

答案对您有帮助吗?

是,对我很有帮助
否,没解决我的问题