.NET調(diào)用新浪微博開放平臺(tái)接口的代碼示例
博客園在新浪微博上開了官方微博(http://t.sina.com.cn/cnblogs),為了方便一些信息的更新,比如IT新聞,我們使用了新浪微博開放平臺(tái)接口。
在這篇文章中,我們將和大家分享如何通過.NET(C#)調(diào)用新浪微博開放平臺(tái)接口。
使用新浪微博開放平臺(tái)接口,需要先申請(qǐng)一帳號(hào),申請(qǐng)方法:給 @微博開放平臺(tái) 發(fā)送私信,或者給open_sina_mblog@vip.sina.com發(fā)郵件,附上您的email,微博個(gè)人主頁(yè),電話,和簡(jiǎn)單介紹。
我們發(fā)了申請(qǐng)郵件后,不到1小時(shí)就收到了申請(qǐng)通過的郵件。然后進(jìn)入新浪微博開放平臺(tái)查看相關(guān)文檔,在文檔中(使用Basic Auth進(jìn)行用戶驗(yàn)證)發(fā)現(xiàn)新浪微博開發(fā)團(tuán)隊(duì)推薦了園子里的Q.Lee.lulu寫的一篇博文:訪問需要HTTP Basic Authentication認(rèn)證的資源的各種語(yǔ)言的實(shí)現(xiàn)。這篇文章成為了我們的重要參考,但該文只是針對(duì)“GET”請(qǐng)求的情況,而新浪微博開放平臺(tái)接口要求HTTP請(qǐng)求方式為“POST”,我們又參考了園子里的烏生魚湯寫的另一篇博文: 使用HttpWebRequest發(fā)送自定義POST請(qǐng)求。在這里感謝兩位園友的分享!
接下來,我們開始.NET調(diào)用新浪微博開放平臺(tái)接口之旅。
1. 首先我們要獲取一個(gè)App Key,在新浪微博開放平臺(tái)的“我的應(yīng)用”中創(chuàng)建一個(gè)應(yīng)用,就會(huì)生成App Key,假設(shè)是123456。
2. 在新浪微博API文檔中找到你想調(diào)用的API,這里我們假定調(diào)用發(fā)表微博的API-statuses/update,url是http://api.t.sina.com.cn/statuses/update.json,POST的參數(shù):source=appkey&status=微博內(nèi)容。其中appkey就是之前獲取的App Key。
3. 準(zhǔn)備數(shù)據(jù)
1) 準(zhǔn)備用戶驗(yàn)證數(shù)據(jù):
- string username = "t@cnblogs.com";
- string password = "cnblogs.com";
- string usernamePassword = username + ":" + password;
username是你的微博登錄用戶名,password是你的博客密碼。
2) 準(zhǔn)備調(diào)用的URL及需要POST的數(shù)據(jù):
- string url = "http://api.t.sina.com.cn/statuses/update.json";
- string news_title = "VS2010網(wǎng)劇合集:講述程序員的愛情故事";
- int news_id = 62747;
- string t_news = string.Format("{0},http://news.cnblogs.com/n/{1}/", news_title, news_id );
- string data = "source=123456&status=" + System.Web.HttpUtility.UrlEncode(t_news);
4. 準(zhǔn)備用于發(fā)起請(qǐng)求的HttpWebRequest對(duì)象:
- System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
- System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
5. 準(zhǔn)備用于用戶驗(yàn)證的憑據(jù):
- System.Net.CredentialCache myCache = new System.Net.CredentialCache();
- myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(username, password));
- httpRequest.Credentials = myCache;
- httpRequest.Headers.Add("Authorization", "Basic " +
- Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));
6. 發(fā)起POST請(qǐng)求:
- httpRequest.Method = "POST";
- httpRequest.ContentType = "application/x-www-form-urlencoded";
- System.Text.Encoding encoding = System.Text.Encoding.ASCII;
- byte[] bytesToPost = encoding.GetBytes(data);
- httpRequest.ContentLength = bytesToPost.Length;
- System.IO.Stream requestStream = httpRequest.GetRequestStream();
- requestStream.Write(bytesToPost, 0, bytesToPost.Length);
- requestStream.Close();
上述代碼成功執(zhí)行后,就會(huì)把內(nèi)容發(fā)到了你的微博上了。
7. 獲取服務(wù)端的響應(yīng)內(nèi)容:
- System.Net.WebResponse wr = httpRequest.GetResponse();
- System.IO.Stream receiveStream = wr.GetResponseStream();
- using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8))
- {
- string responseContent = reader.ReadToEnd();
- }
好了,.NET調(diào)用新浪微博開放平臺(tái)接口之旅就完成了,很簡(jiǎn)單吧。
如果沒有Q.Lee.lulu與烏生魚湯的文章作為參考,對(duì)我們來說就不會(huì)這么輕松,這也許就是分享的價(jià)值吧,你的一點(diǎn)小經(jīng)驗(yàn)卻可能給別人帶來很大的幫助。
所以,我們也來分享一下,雖然不算什么經(jīng)驗(yàn),只是一個(gè)整理,但也許會(huì)對(duì)需要的人有幫助。
相關(guān)鏈接:sarlmolapple寫了個(gè)C#的SDK:http://code.google.com/p/opensinaapi/
原文鏈接:http://www.cnblogs.com/cmt/archive/2010/05/13/1733904.html
【編輯推薦】