淘寶API開發系列---淘寶API的測試及使用2


繼續前面一篇隨筆《淘寶API開發系列---淘寶API的測試及使用》,來繼續介紹淘寶API的具體代碼開發部分,上篇主要是介紹淘寶SDK開發的一些流程及必備的信息,以及掌握如何學會利用API文檔、淘寶API測試工具來獲取我們所需的數據,其中我一般傾向於獲取Json數據,然后進行分析,其中Json數據可以通過JsonView工具(http://jsonviewer.codeplex.com/)進行格式化顯示,方便我們了解和區分各個屬性的信息。另外淘寶的SDK里面,封裝了很多對象,我們通過數據就可以獲取到相關的信息了,不過注意的就是,我們每個接口調用,都要傳入Fields的屬性,如果我們沒有指定相應的屬性字段,那么接口返回的數據,就沒有這項的,淘寶SDK的對象屬性就會為空。

 

 通過以上的工具,我們就能可視化屬性的信息了,對接口數據的理解就更加清晰,首先我們來看看我測試例子的運行效果如下所示。

 

 其中上面例子的代碼如下所示。

         private  void TestUserGet()
        {
             // 單獨用戶的信息
            Console.WriteLine( " 單獨用戶的信息 ");
            UserGetRequest req =  new UserGetRequest();  // 實例化具體API對應的Request類
            req.Fields =  " user_id,nick,created,buyer_credit,type,sex ";
            req.Nick =  " wuhuacong ";
            UserGetResponse rsp = myclient.Execute(req); // 執行API請求並將該類轉換為response對象
            Console.WriteLine(rsp.Body);
             if (rsp.User !=  null)
            {
                 // Console.WriteLine(rsp.User.Nick);
                
// Console.WriteLine(ObjectToString(rsp.User));
                List<User> list =  new List<User>();
                list.Add(rsp.User);
                 this.winGridView1.DisplayColumns = req.Fields.Replace( " _ """); // 對應字段的屬性沒有“_”字符的
                 this.winGridView1.DataSource = list;
            }
        }

         private  void TestItemGet()
        {
             // 單獨商品的信息
            Console.WriteLine( " 單獨商品的信息 ");
            ItemGetRequest req =  new ItemGetRequest();
            req.Fields =  " num_iid,title,nick,pic_path,cid,price,type,location.city,delist_time,post_fee ";
            req.NumIid =  3838293428L;
            ItemGetResponse itemRsp = myclient.Execute(req);
             if (itemRsp !=  null && itemRsp.Item !=  null)
            {
                 // Console.WriteLine(itemRsp.Item.Nick);
                
// Console.WriteLine(ObjectToString(itemRsp.Item));
                List<Item> list =  new List<Item>();
                list.Add(itemRsp.Item);
                 this.winGridView1.DisplayColumns = req.Fields.Replace( " _ """); // 對應字段的屬性沒有“_”字符的
                 this.winGridView1.DataSource = list;
            }
        }

         private  void TestItemSearch()
        {
             // 查詢商品信息(不含類別)
            Console.WriteLine( " 查詢商品信息(不含類別) ");
            ItemsGetRequest req =  new ItemsGetRequest();
            req.Fields =  " num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume "; // ,location.city,location.state";
            req.Q =  " 筆記本 ";
             // itemReq.Cid = "14";
            req.OrderBy =  " volume:desc ";
            req.PageNo =  1;
            req.PageSize =  40;

             // 顯示列表信息
            ItemsGetResponse itemRsp = myclient.Execute(req);
             if (itemRsp !=  null)
            {
                 // Console.WriteLine(itemRsp.TotalResults);
                
// foreach (Item item in itemRsp.Items)
                
// {
                
//     Console.WriteLine(ObjectToString(item));
                
// }
                 this.winGridView1.DisplayColumns = req.Fields.Replace( " _ """); // 對應字段的屬性沒有“_”字符的
                 this.winGridView1.DataSource = itemRsp.Items;
            }

        } 

對於需要獲取用戶私密信息,如買入賣出等重要信息,還需要獲取用戶的SessionKey的,我們可以通過下面接口函數,彈出登錄窗口,然后登錄后,定位到對應的App應用頁面,然后頁面加載的時候,獲取到對應的SessionKey、

 

 

 

 

         ///   <summary>
        
///  判斷是否順利獲取SessionKey
        
///   </summary>
        
///   <returns></returns>
         private  string GetAuthorizeCode( string appKey)
        {
             string authorizeCode =  "";
            FrmAuthorized dlg =  new FrmAuthorized();
            dlg.AppKey = appkey;
             if (dlg.ShowDialog() == DialogResult.OK)
            {
                authorizeCode = dlg.AuthrizeCode;
            }
             if ( string.IsNullOrEmpty(authorizeCode))  return  null;

             string sessionKeyUrl =  string.Format(TOP_AUTH_URL, authorizeCode);
            HttpHelper helper =  new HttpHelper();
             string html = helper.GetHtml(sessionKeyUrl);
             // 格式
            
// top_appkey=1142&top_parameters=xxx&top_session=xxx&top_sign=xxx&encode=utf-8
             string reg =  " .*?&top_session=(?<session>.*?)&top_sign ";
             string sessionKey = CRegex.GetText(html, reg,  1);
             return sessionKey;

        } 

 最后我們看看其中獲取已買記錄的接口實現如下所示。

         private  void TestBuyInfo()
        {
             if ( string.IsNullOrEmpty(sessionKey))
            {
                sessionKey = GetAuthorizeCode( this.appkey);
            }

             // 買入交易
            Console.WriteLine( " 買入交易 ");
            TradesBoughtGetRequest req =  new TradesBoughtGetRequest();
             // req.Fields = "tid,title,price,type,num_iid,seller_nick,buyer_nick,status";
            req.Fields =  " tid,title,price,type,num_iid,seller_nick,buyer_nick,status,receiver_state,receiver_city,receiver_district,receiver_address ";
            req.PageNo =  1L;
            req.PageSize =  40L;
            TradesBoughtGetResponse rsp = myclient.Execute(req, sessionKey);
             if (rsp !=  null)
            {
                 // Console.WriteLine(rsp.Trades.Count);
                
// if (rsp.Trades.Count > 0)
                
// {
                
//     foreach (Trade item in rsp.Trades)
                
//     {
                
//         Console.WriteLine(ObjectToString(item));
                
//     }
                
// }
                 this.winGridView1.DisplayColumns = req.Fields.Replace( " _ """); // 對應字段的屬性沒有“_”字符的
                 this.winGridView1.DataSource = rsp.Trades;
            }

        } 

 以上利用了我的Winform分頁控件進行數據展示,因此代碼要簡化一些,當然,也可以用DataGridView來進行數據顯示,不過代碼方面可能要多一些。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM