本次使用的是淘宝沙箱数据(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"); } } } }