本次使用的是淘寶沙箱數據(http://mini.tbsandbox.com/),淘寶提供了一些測試賬號(sandbox_c_1 密碼 taobao1234 http://www.tbsandbox.com/doc/)
1.Default.aspx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Top.Api; using Top.Api.Request; using Top.Api.Response; using Top.Api.Domain; namespace Demo.SandBox { public partial class Default : System.Web.UI.Page { //http://gw.api.taobao.com/router/rest 正式 protected const string API_URL = "http://gw.api.tbsandbox.com/router/rest";//測試沙箱環境地址 protected const string APP_KEY = "APP_KEY";//申請的APP_KEY protected const string APP_SECRET = "APP_SECRET";//申請的APP_SECRET protected void Page_Load(object sender, EventArgs e) { //SESSION_KEY是通過回調地址獲取的。(手動獲取地址http://api.taobao.com/apitools/sessionPage.htm) if (Session["SESSION_KEY"] != null) { ITopClient client = new DefaultTopClient(API_URL, APP_KEY, APP_SECRET); TradesSoldGetRequest req = new TradesSoldGetRequest(); req.Fields = "seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type"; DateTime dateTime = DateTime.Parse("2013-07-14 00:00:00"); req.StartCreated = dateTime; DateTime dateTime1 = DateTime.Parse("2013-07-15 23:59:59"); req.EndCreated = dateTime1; TradesSoldGetResponse rsp = client.Execute(req, Session["SESSION_KEY"].ToString()); string errMsg = rsp.ErrMsg; foreach (var item in rsp.Trades) { Response.Write(" 訂單編號:" + item.Tid + " 買家名稱:" + item.BuyerNick + " 交易狀態:" + GetOrderStatus(item.Status) + "<br/>"); } Response.Write("合計:" + rsp.TotalResults.ToString()); } else { //http://container.open.taobao.com/container?appkey= + APP_KEY //正式地址 Response.Redirect("http://container.api.tbsandbox.com/container?appkey=" + APP_KEY);//授權獲取SESSION_KEY } } /// <summary> /// 獲取訂單狀態 /// </summary> /// <param name="str"></param> /// <returns></returns> public string GetOrderStatus(string str) { string result = string.Empty; switch (str) { case "TRADE_NO_CREATE_PAY": result = "沒有創建支付寶交易"; break; case "WAIT_BUYER_PAY": result = "等待買家付款"; break; case "WAIT_SELLER_SEND_GOODS": result = "買家已付款"; break; case "SELLER_CONSIGNED_PART": result = "賣家部分發貨"; break; case "WAIT_BUYER_CONFIRM_GOODS": result = "賣家已發貨"; break; case "TRADE_BUYER_SIGNED": result = "買家已簽收"; break; case "TRADE_FINISHED": result = "交易成功"; break; case "TRADE_CLOSED": result = "交易關閉"; break; case "TRADE_CLOSED_BY_TAOBAO": result = "交易被淘寶關閉"; break; default: result = ""; break; } return result; } } }
2.OAuth.aspx 通過回調地址獲取SessionKey
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Demo.SandBox { public partial class OAuth : System.Web.UI.Page { /// <summary> /// 淘寶Top_session /// </summary> public string Top_session { get { return !string.IsNullOrEmpty(Request.QueryString["top_session"]) ? Request.QueryString["top_session"] : ""; } } protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Top_session)) { Session["SESSION_KEY"] = Top_session; Response.Redirect("~/SandBox/Default.aspx"); } } } }