65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 GData API 将博客集成到 ASP.NET 网站中

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6投票s)

2012年7月16日

CPOL

3分钟阅读

viewsIcon

40354

downloadIcon

2561

使用 Blogger 博客和 Google GDI API 管理 ASP.NET 网站的内容

引言

许多个人和公司的网站主要由静态页面组成,但有时需要向客户提供更新或有关公司新闻的信息。大多数此类网站都需要实际更新代码才能发布这些新闻项目或更新。对于较小的公司,一种动态的方式来维护这些页面可以集成到博客网站中,通过这种方式,博客的内容会自动提取到要更新的页面中。这提供了一种管理这些页面内容的方法,而无需触及网站上的代码或进行部署。对博客的更新直接反映在目标页面中。本文使用 blogger 网站和 ASP.NET 网站提供此类功能的实现。 在该网站的其他地方,讨论了将博客集成到 ASP.NET blogger 中,并提供了 FTP 解决方案,但此功能正在逐步淘汰。我的解决方案涉及使用 Google 的 GData 服务 API 来查询博客,并在 ASP.NET 网站中动态连接并显示它们,尽管没有理由不能在其他类型的网站中使用它。

环境

Incede.BloggerInteg.dll 是用 C#3.5 编写的,示例网页在 VS2008 中设计。 应该可以轻松升级到 VS2010。

使用代码

该项目实现为一个自定义 DLL (Incede.BloggerInteg.dll),该组件具有 BlogService 类,该类具有一个公共方法 GetUserBlogs(),该方法返回在指定的 blogger 网站上发布的博客列表。

BlogService 服务类的构造函数接受博客名称、用户 ID、博客密码作为参数。 构造函数中还包括要提取的博客条目数量。该组件的核心是来自 Google 的两个 API,用于访问 blogger 系统。

  • Google.GData.Blogger
  • Google.GData.Client

这些组件提供了必要的在线身份验证服务到 blogger 平台,以及查询用户博客的功能。 GetUserBlogs 首先使用 Google GDataCredentials 创建服务凭据的实例,并且服务正在使用 GDATA 和博客 URI 获取博客条目。

public List<blogentry> GetUserBlogs()
{
    if (blogname.Trim().Equals(String.Empty))
        blogname = "incedeit.blogspot.com";//default if not supplied
    if (numberOfEntries <= 0)
        numberOfEntries = 10; //default

    var gService = new Google.GData.Client.Service("blogger", blogname);
    gService.Credentials = new GDataCredentials(username, password);

    Uri blogPostUri = SelectUserBlog(gService, blogname)
    List<blogentry> blogs = GetBlogEntryContent(gService, 
    blogPostUri,   numberOfEntries);
    return blogs;
}

这是执行识别用户博客的核心过程的方法。 首先从查询创建 atomfeed,然后将条目过滤到用户博客。此用户博客由静态 URI 标识。

static Uri SelectUserBlog(Service service, string blogname)
{
    //blogname = incedeit.blogspot.com example 
    FeedQuery query = new FeedQuery();
    // Retrieving a list of blogs
    query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
    AtomFeed feed = service.Query(query);

    // Publishing a blog post
    Uri blogPostUri = null;
    if (feed != null)
    {
        foreach (AtomEntry entry in feed.Entries)
        {
            //iterate thru blogs 

            if (entry.AlternateUri.Content.Contains(blogname))
            {
                // find the href in the link with a rel pointing to the blog's feed
                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());
                    }
                }

                return blogPostUri;
            }
        }
    }
    return blogPostUri;
}

此方法最终遍历已标识的用户博客 URI 的条目,并从博客中提取相关内容,例如标题、内容和页脚。

static List<blogentry> GetBlogEntryContent(Service service, Uri blogUri, int numberOfEntries)
{
    List<blogentry> allblogs = new List<blogentry>();
    if (blogUri != null)
    {

        // Retrieve all posts in a blog
        FeedQuery query = new FeedQuery();
        query.Uri = blogUri;
        query.NumberToRetrieve = numberOfEntries; //get last x number of posts
        AtomFeed feed = service.Query(query);
        foreach (AtomEntry entry in feed.Entries)
        {
            BlogEntry b = new BlogEntry();
            b.Title = entry.Title.Text;
            b.content = entry.Content.Content;
            b.footer = String.Format("Posted on {0}", entry.Published.ToString());
            allblogs.Add(b);
        }
    }
    return allblogs;
}

用法

要使用该组件,我们只需创建服务 Incede.BloggerInteg.BlogService 的一个实例,并为其提供几个参数,即博客名称、凭据以及我们感兴趣的提取的博客条目数量。 这些参数在我们的示例项目的 web.config 中定义。 服务返回一个包含博客条目类的 IList,其中包含诸如博客标题、博客内容和 footer.ShowBlogEntries 之类的属性,它只是将博客条目格式化为用于显示的 data list。

Incede.BloggerInteg.BlogService Biservicc = new Incede.BloggerInteg.BlogService(blogname, userid,pwd,int.Parse(entries));
 
List<incede.bloggerinteg.blogentry> blogs =   Biservicc.GetUserBlogs();
ShowBlogEntries(blogs, blogname);

整合

  1. bin 目录应包含以下支持 DLL
    • Google.GData.Blogger.dll
    • Google.GData.Client.dll
  2. 您的 web.config 应该具有以下条目,用于指向博客网站的 URL 和凭据
    • blogName
    • blogUserid
    • blogPass
    • noOfEntries
  3. 如果需要,您可以修改核心服务以从 blogger 网站返回其他参数。 我的实现具有所需的大多数  基本项目。 

示例项目 

我为此项目使用了一个我创建的示例博客来说明示例项目中的概念。 实际的博客网站是 http://incedeblogger.blogspot.com。 对博客的任何更新都会自动提取到示例项目网页中。 可以在我的网站上看到此功能的实际实现 http://www.incedeit.com/main/UI/News.aspx

集成页面的屏幕截图: 

 

历史

  • 初始帖子 2012 年 7 月 15 日。
© . All rights reserved.