有許多人都使用 webBrowser 來模擬瀏覽器的動作發布文章,但是 blogger 無法讓你這樣做,你可以試試看,保證你抓不到 tag。
未下載 GData APIs 且未安裝請參考:GData APIs ( Google Data APIs ) 介紹與安裝。
引用參考
使用 Google.GData.Client.dll 。using Google.GData.Client;
建立 Service
username 為帳號 ( Email ),password 為密碼,若帳號有設置兩步驗證的話,請到 應用程式專用密碼 去取得一組密碼,要不然原本帳號密碼是無法使用。service = new Service("blogger", "blogger-example");
service.Credentials = new GDataCredentials(username, password);
取得文章列表
max-results 為參數,讓回傳的文章數最大為 500 篇。/* 列出使用者的部落格 */
public Dictionary<string, Uri> GetListUserBlogs()
{
Dictionary<string, Uri> dicBlogs = new Dictionary<string,Uri>();
FeedQuery query = new FeedQuery();
query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
AtomFeed feed = service.Query(query);
// 發佈文章
Uri blogPostUri = null;
if (feed != null)
{
foreach (AtomEntry entry in feed.Entries)
{
for (int i = 0; i < entry.Links.Count; i++)
{
if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
{
blogPostUri = new Uri(entry.Links[i].HRef.ToString() + "?max-results=500");
dicBlogs.Add(entry.Title.Text, blogPostUri);
}
}
// return blogPostUri;
}
}
return dicBlogs;
}
新增文章
其中 tags 變數為逗點分隔的字串,isDraft 為是否為草稿。/* 建立新文章且將它送到 blogPostUri */
public AtomEntry CreatePost(Uri blogPostUri, string title, string content, string tags, DateTime dt, bool isDraft)
{
AtomEntry createdEntry = null;
if (blogPostUri != null)
{
// construct the new entry
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = title;
newPost.Content = new AtomContent();
newPost.Content.Content = content;
foreach (string term in tags.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
newPost.Categories.Add(new AtomCategory(term, new AtomUri("http://www.blogger.com/atom/ns#")));
newPost.Published = dt;
newPost.IsDraft = isDraft;
createdEntry = service.Insert(blogPostUri, newPost);
if (createdEntry != null)
{
Console.WriteLine(" New blog post created with title: " + createdEntry.Title.Text);
}
}
return createdEntry;
}
更新文章
/* 編輯文章 */
public void EditEntry(AtomEntry toEdit)
{
if (toEdit != null)
{
// toEdit.Title.Text = "Marriage Woes!";
toEdit = toEdit.Update();
}
}
刪除文章
/* 刪除文章 */
public void DeleteEntry(AtomEntry toDelete)
{
if (toDelete != null)
{
toDelete.Delete();
}
}


沒有留言 :
張貼留言