How to get Google plus posts of user using C#

In this blog post, we will get to know how to retrieve google plus posts of a particular user.

Let’s get started by creating a class which will allow us to store each item of stream (post) data within a list.

public class GooglePlusPost
{
    public string Title { get; set; }
    public string Text { get; set; }
    public string PostType { get; set; }
    public string Url { get; set; }
}

Now, we will create a method which will retrieve all streams of google plus for a particular user profile.

public class GooglePlus
{
    private static string ProfileID = ConfigurationManager.AppSettings["googleplus.profileid"].ToString();
    
    public static List<GooglePlusPost> GetPosts(int max)
    {
        try
        {
            var service = new PlusService();
            service.Key = GoogleKey;
            var profile = service.People.Get(ProfileID).Fetch();

            var posts = service.Activities.List(ProfileID, ActivitiesResource.Collection.Public);
            posts.MaxResults = max;

            List<GooglePlusPost> postList = new List<GooglePlusPost>();

            foreach (Activity a in posts.Fetch().Items)
            {
                GooglePlusPost gp = new GooglePlusPost();

                //If the post contains your own text, use this otherwise look for
                //text contained in the post attachment.
                if (!String.IsNullOrEmpty(a.Title))
                {
                    gp.Title = a.Title;
                }
                else
                {
                    //Check if post contains an attachment
                    if (a.Object.Attachments != null)
                    {
                        gp.Title = a.Object.Attachments[0].DisplayName;
                    }
                }

                gp.PostType = a.Object.ObjectType; //Type of post
                gp.Text = a.Verb;
                gp.Url = a.Url; //Post URL

                postList.Add(gp);
            }

            return postList;
        }
        catch
        {
            return new List<GooglePlusPost>();
        }
    }
}

 

Using this method, we can fetch the feed from Google plus profile of a particular user. You can try caching this request since the Google plus API has a limit.

 

Thanks for dropping by !! Feel free to comment to this post or you can also reach me at naik899@gmail.com.

Leave a Reply

Your email address will not be published. Required fields are marked *