針對淘寶開放平臺接口的調用,用SDK比較方便。 但如果不想引用淘寶的SDK,以更加簡單的方式該如何調用呢?
下面用taobao.trades.sold.get(獲取淘寶訂單交易記錄)這個API來說明一下拼接方式。
淘寶OpenAPI服務器地址: TaoBaoAPIUrl = "http://gw.api.taobao.com/router/rest";
開發者申請的應用標識:Appkey = "1";
開發者申請的應用標識關聯的密鑰:Appsecret = "2";
用戶針對當前應用授權的會話密鑰:SessionKey = "3";
調用時:
1,基本參數無需更改,
2,API相關參數的method對應的API名稱,fields對應的是當前API要求返回的字段,Status對應的API允許傳入的字段。
public static String TaoBaoAPIUrl = "http://gw.api.taobao.com/router/rest";
public static String Appkey = "***";
public static String Appsecret = "***";
public static String SessionKey = "***";
static void Main(string[] args)
{
SortedDictionary<string, string> dict = new SortedDictionary<string, string>();
#region 基本參數
dict.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
dict.Add("v", "2.0");
dict.Add("app_key", Appkey);
dict.Add("partner_id", "top-apitools");
dict.Add("format", "json");
dict.Add("session", SessionKey);
dict.Add("sign_method", "md5");
#endregion
#region API相關參數
dict.Add("method", "taobao.trades.sold.get");
dict.Add("fields", "buyer_nick");
dict.Add("status", "WAIT_BUYER_PAY");
#endregion
#region 生成MD5簽名
String _SignString = Appsecret + String.Join("", dict.Select(a => a.Key + a.Value).ToArray()) + Appsecret;
Byte[] _SignData = Encoding.UTF8.GetBytes(_SignString);
Byte[] _DataMD5 = MD5.Create().ComputeHash(_SignData);
String Sign = String.Join("", _DataMD5.Select(a => a.ToString("X2")).ToArray());
dict.Add("sign", Sign);
#endregion
//最終請求地址
String url = TaoBaoAPIUrl + "?" + String.Join("&", dict.Select(a => a.Key + "=" + a.Value).ToArray());
}