MetaWeblog API調用

http://rpc.cnblogs.com/metaweblog/webenhweb

 

 

在網上閒逛,忽然對博客的接口感興趣,經考察,多數博客都對metaWeblog Api 提供了支持,雖然windows live writer是好用,不過出於對這個接口的好奇,也想本身作個能發博客的小工具.windows

處處瀏覽資料,用了一個下午終於成功發佈了一篇簡陋版博客:)。其實很簡單,方法對了很快就連上了。數據結構

MetaWeblog API中文說明app

一、什麼是MetaWeblog API?函數

MetaWeblog API(MWA)是一個Blog程序接口標準,容許外部程序來獲取或者設置Blog的文字和熟悉。他創建在XMLRPC接口之上,而且已經有了不少的實現。工具

二、基本的函數規範post

有三個基本的函數規範:網站

metaWeblog.newPost (blogid, username, password, struct, publish) 返回一個字符串,多是Blog的ID。
metaWeblog.editPost (postid, username, password, struct, publish) 返回一個Boolean值,表明是否修改爲功。
metaWeblog.getPost (postid, username, password) 返回一個Struct。this

其中blogid、username、password分別表明Blog的id(註釋:若是你有兩個Blog,blogid指定你須要編輯的blog)、用戶名和密碼。url

 

1、要引用的DLL【網上提供的示例多包含了這個DLL】

image

2、要實現的數據結構

 MetaWeblogCode【如不想看代碼請自行摺疊】

複製代碼
  1 using System;
  2 using CookComputing.XmlRpc;
  3 
  4 namespace metaWeblogTest
  5 {
  6 
  7     #region 微軟MSN網站 使用的 MetaWeblog API.
  8     /// 這個結構表明用戶的博客基本信息
  9     /// </summary>
10     [XmlRpcMissingMapping(MappingAction.Ignore)]
11     public struct UserBlog
12     {
13         public string url;
14         public string blogid;
15         public string blogName;
16     }
17 
18 
19     /// <summary> 
20     /// 這個結構表明用戶信息
21     /// </summary> 
22     [XmlRpcMissingMapping(MappingAction.Ignore)]
23     public struct UserInfo
24     {
25         public string url;
26         public string blogid;
27         public string blogName;
28         public string firstname;
29         public string lastname;
30         public string email;
31         public string nickname;
32     }
33 
34 
35     /// <summary> 
36     /// 這個結構表明博客分類信息
37     /// 這後面的getCategories()方法會取到CATEGORY數據。
38     /// </summary> 
39     [XmlRpcMissingMapping(MappingAction.Ignore)]
40     public struct Category
41     {
42         public string description;
43         public string title;
44     }
45 
46     /// <summary>  47     /// 這個結構表明博客( 文章 )信息。 48     /// 這後面的 editPost()方法, getRecentPosts()方法 和 getPost()方法 會取倒POST數據 .  49     /// </summary>  50     [XmlRpcMissingMapping(MappingAction.Ignore)] 51     public struct Post 52     { 53         public DateTime dateCreated; 54         public string description; 55         public string title; 56         public string postid; 57         public string[] categories; 58     } 59     #endregion 60  61  62     #region 網站:http://msdn.microsoft.com/en-us/library/aa905670.aspx 63     ///// <summary>  64     ///// 微軟MSN網站 使用的 MetaWeblog API.  65     ////  網站:http://msdn.microsoft.com/en-us/library/aa905670.aspx 66     ///// </summary>  67     public class M_MetaWeblog : XmlRpcClientProtocol 68     { 69  70  71         /// <summary>  72         /// Returns the most recent draft and non-draft blog posts sorted in descending order by publish date.  73         /// </summary>  74         /// <param name="blogid"> This should be the string MyBlog, which indicates that the post is being created in the user’s blog. </param>  75         /// <param name="username"> The name of the user’s space. </param>  76         /// <param name="password"> The user’s secret word. </param>  77         /// <param name="numberOfPosts"> The number of posts to return. The maximum value is 20. </param>  78         /// <returns></returns>  79         /// TODO:獲得最近發佈的帖子 80         [XmlRpcMethod("metaWeblog.getRecentPosts")] 81         public Post[] getRecentPosts( 82         string blogid, 83         string username, 84         string password, 85         int numberOfPosts) 86         { 87  88             return (Post[])this.Invoke("getRecentPosts", new object[] { blogid, username, password, numberOfPosts }); 89         } 90  91  92         /// <summary>  93         /// Posts a new entry to a blog.  94         /// </summary>  95         /// <param name="blogid"> This should be the string MyBlog, which indicates that the post is being created in the user’s blog. </param>  96         /// <param name="username"> The name of the user’s space. </param>  97         /// <param name="password"> The user’s secret word. </param>  98         /// <param name="post"> A struct representing the content to update. </param>  99         /// <param name="publish"> If false, this is a draft post. </param>  100         /// <returns> The postid of the newly-created post. </returns>  101         /// TODO:增長一個最新的帖子 102         [XmlRpcMethod("metaWeblog.newPost")] 103         public string newPost( 104         string blogid, 105         string username, 106         string password, 107         Post content, 108         bool publish) 109         { 110  111             return (string)this.Invoke("newPost", new object[] { blogid, username, password, content, publish }); 112         } 113  114         /// <summary>  115         /// Edits an existing entry on a blog.  116         /// </summary>  117         /// <param name="postid"> The ID of the post to update. </param>  118         /// <param name="username"> The name of the user’s space. </param>  119         /// <param name="password"> The user’s secret word. </param>  120         /// <param name="post"> A struct representing the content to update. </param>  121         /// <param name="publish"> If false, this is a draft post. </param>  122         /// <returns> Always returns true. </returns>  123         /// TODO:更新一個帖子 124         [XmlRpcMethod("metaWeblog.editPost")] 125         public bool editPost( 126         string postid, 127         string username, 128         string password, 129         Post content, 130         bool publish) 131         { 132  133             return (bool)this.Invoke("editPost", new object[] { postid, username, password, content, publish }); 134         } 135  136         /// <summary>  137         /// Deletes a post from the blog.  138         /// </summary>  139         /// <param name="appKey"> This value is ignored. </param>  140         /// <param name="postid"> The ID of the post to update. </param>  141         /// <param name="username"> The name of the user’s space. </param>  142         /// <param name="password"> The user’s secret word. </param>  143         /// <param name="post"> A struct representing the content to update. </param>  144         /// <param name="publish"> This value is ignored. </param>  145         /// <returns> Always returns true. </returns>  146         /// TODO:刪除一個帖子 147         [XmlRpcMethod("blogger.deletePost")] 148         public bool deletePost( 149         string appKey, 150         string postid, 151         string username, 152         string password, 153         bool publish) 154         { 155  156             return (bool)this.Invoke("deletePost", new object[] { appKey, postid, username, password, publish }); 157         } 158  159  160         /// <summary>  161         /// Returns information about the user’s space. An empty array is returned if the user does not have a space.  162         /// </summary>  163         /// <param name="appKey"> This value is ignored. </param>  164         /// <param name="postid"> The ID of the post to update. </param>  165         /// <param name="username"> The name of the user’s space. </param>  166         /// <param name="password"></param> 167         /// <returns> An array of structs that represents each of the user’s blogs. The array will contain a maximum of one struct, since a user can only have a single space with a single blog. </returns>  168         /// TODO:獲得用戶的博客清單 169         [XmlRpcMethod("blogger.getUsersBlogs")] 170         public UserBlog[] getUsersBlogs( 171         string appKey, 172         string username, 173         string password) 174         { 175  176             return (UserBlog[])this.Invoke("getUsersBlogs", new object[] { appKey, username, password }); 177         } 178  179         /// <summary>  180         /// Returns basic user info (name, e-mail, userid, and so on).  181         /// </summary>  182         /// <param name="appKey"> This value is ignored. </param>  183         /// <param name="postid"> The ID of the post to update. </param>  184         /// <param name="username"> The name of the user’s space. </param>  185         /// <param name="password"></param> 186         /// <returns> A struct containing profile information about the user.  187         /// Each struct will contain the following fields: nickname, userid, url, e-mail,  188         /// lastname, and firstname. </returns>  189         /// TODO:獲得用戶信息 190         [XmlRpcMethod("blogger.getUserInfo")] 191         public UserInfo getUserInfo( 192         string appKey, 193         string username, 194         string password) 195         { 196  197             return (UserInfo)this.Invoke("getUserInfo", new object[] { appKey, username, password }); 198         } 199  200  201         /// <summary>  202         /// Returns a specific entry from a blog.  203         /// </summary>  204         /// <param name="postid"> The ID of the post to update. </param>  205         /// <param name="username"> The name of the user’s space. </param>  206         /// <param name="password"> The user’s secret word. </param>  207         /// <returns> Always returns true. </returns>  208         /// TODO:獲取一個帖子 209         [XmlRpcMethod("metaWeblog.getPost")] 210         public Post getPost( 211         string postid, 212         string username, 213         string password) 214         { 215  216             return (Post)this.Invoke("getPost", new object[] { postid, username, password }); 217         } 218  219         /// <summary>  220         /// Returns the list of categories that have been used in the blog.  221         /// </summary>  222         /// <param name="blogid"> This should be the string MyBlog, which indicates that the post is being created in the user’s blog. </param>  223         /// <param name="username"> The name of the user’s space. </param>  224         /// <param name="password"> The user’s secret word. </param>  225         /// <returns> An array of structs that contains one struct for each category. Each category struct will contain a description field that contains the name of the category. </returns>  226         /// TODO:獲得博客分類 227         [XmlRpcMethod("metaWeblog.getCategories")] 228         public Category[] getCategories( 229         string blogid, 230         string username, 231         string password) 232         { 233  234             return (Category[])this.Invoke("getCategories", new object[] { blogid, username, password }); 235         } 236     } 237     #endregion 238 } 239 
複製代碼

3、圖示【調用API發送第一篇簡單博客】

 image

 

——————————————————————————————————————————

資源連接:

http://www.xmlrpc.com/spec   英文的xml-rpc規範
RFC: MetaWeblog API   MetaWeblog API 規範
http://www.duduwolf.com/post/41.asp  中文翻譯的xml-rpc規範(感謝翻譯者:滴水)
http://www.XML-RPC.Net XML-RPC的.NET 實現,其中有最新.net2.0的XML-RPC實現的下載
MetaWeblogAPI and MSN Spaces  MSDN上關於MetaWeblog API及MSN Spaces接口的說明及.NET示例

相關文章
相關標籤/搜索