http://www.cnblogs.com/yangm/p/3205770.html
http://www.cnblogs.com/yangm/p/3205791.html
为了进行淘宝的API开发,首先我们需要做下面几件事情。
1.开发者注册一个账号。(http://open.taobao.com/)
2.然后为每个淘宝应用注册一个应用程序键(App Key和App Secret) 。
3.下载淘宝API的SDK并掌握基本的API基础知识和调用,具体可以参考论坛的信息(http://dev.open.taobao.com/bbs/forum.php )里面的技术分享区等。
4.利用SDK接口和对象,传入AppKey和App Key和App Secret或者必要的时候获取并传入SessionKey来进行程序开发。
5.利用淘宝平台的文档中心和API测试工具,对接口进行测试。从而了解返回信息,方便程序获取。(http://api.taobao.com/apitools/apiTools.htm)
淘宝提供了买家和卖家2种应用方式,卖家审核比较严格需要营业执照、产品说明书等。另外淘宝还提供了沙箱测试环境(http://mini.tbsandbox.com/),
在里面可以开店买卖东西。
本次使用的是淘宝沙箱数据(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"); } } } }