Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created October 23, 2011 10:12
Show Gist options
  • Save nissuk/1307206 to your computer and use it in GitHub Desktop.
Save nissuk/1307206 to your computer and use it in GitHub Desktop.
C#: Bloggerに新しいエントリを投稿する例
using System;
using System.Text;
using System.Linq;
// C:\Program Files (x86)\Google\Google Data API SDK\Redist から参照します
using Google.GData.Client;
namespace Example
{
static class BloggerPost
{
/// <summary>
/// アカウントに紐付けられているBlogの中で、最初に見つかったBlogの投稿URIを取得します。
/// </summary>
/// <param name="service">Google.GData.Client.Service</param>
/// <returns>最初に見つかったBlogの投稿Uri。見つからなければnull</returns>
static Uri GetFirstBlogPostUri(this Service service)
{
var query = new FeedQuery("http://www.blogger.com/feeds/default/blogs");
var feed = service.Query(query);
if (feed == null) return null;
if (feed.Entries.Count < 1) return null;
var link = feed.Entries[0].Links.Where(x => x.Rel.Equals("http://schemas.google.com/g/2005#post")).First();
if (link == null) return null;
return new Uri(link.HRef.ToString());
}
static void Main(string[] args)
{
var username = ""; // 例えば [email protected]
var password = ""; // 例えば secretPassword
// Serviceを生成します。
var service = new Service("blogger", "blogger-example") {
Credentials = new GDataCredentials(username, password)
};
((GDataGAuthRequestFactory)service.RequestFactory).AccountType = "GOOGLE";
// Entryを生成します。
var newPost = new AtomEntry();
newPost.Title.Text = "Test";
newPost.Content = new AtomContent() {
Content = @"<div xmlns='http://www.w3.org/1999/xhtml'>
<p>Test with Blogger API(C#)</p>
</div>",
Type = "xhtml"
};
// Blogに投稿します。
var postUri = service.GetFirstBlogPostUri();
if (postUri == null) return;
service.Insert(postUri, newPost);
}
}
}
@nissuk
Copy link
Author

nissuk commented Dec 23, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment