Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET

How to Use Google+ APIs from ASP.NET - Samples and Source Code

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
18 Sep 2014CPOL2 min read 13.6K   4  
How to use Google+ APIs from ASP.NET - Samples and source code

Google released Google+ platform last month and announced some APIs few days ago to integrate Google+ with custom applications. The initial version of Google+ APIs can be used to retrieve public data from Google+ system. Future version of Google+ APIs will support more advanced operations like posting to your Google+ profile or Google+ page, retrieving private information, etc.

Today, I was trying to integrate Google+ with our websites to retrieve and show the list of our pages shared in Google+. I quickly realized the data retrieved using the Google+ APIs are not strongly typed as I would expect in .NET. Instead, Google+ APIs return data in JSON format, which is a structured form of data, but not strongly typed like .NET types.

I tried couple of Google+ APIs. After couple of trial and errors, I was able to call some Google+ APIs, using the WebRequest class in .NET.

Here is the code I used to call Google+ API:

JavaScript
string requestString = "https://www.googleapis.com/plus/v1/people/" + _profileId + "?key=" + _apiKey;
WebRequest objWebRequest = WebRequest.Create(requestString);
WebResponse objWebResponse = objWebRequest.GetResponse();

Stream objWebStream = objWebResponse.GetResponseStream();

using (StreamReader objStreamReader = new StreamReader(objWebStream))
{
    string result = objStreamReader.ReadToEnd();

    return result;
}

In the above code, _profileId represents a valid Google+ profile. It does not need to be any valid profile id. This API returns only public information from any profile and so no authentication is required to call this. However, you need to pass an API Key before you can call the Google+ APIs. That is what I used in the variable _apiKey. You can get a Google+ API Key from here.

The above code returns some text data in JSON format, which is structured data.

We need to convert the data in to valid .NET types. The JavaScriptSerializer class comes in handy here. We can use this class to convert the JSON data structure to corresponding C# or VB.NET class.

Example:

JavaScript
JavaScriptSerializer js = new JavaScriptSerializer();
string requestString = "https://www.googleapis.com/plus/v1/people/" 
                  + _profileId + "?key=" + _apiKey;
string result = GetWebData(requestString);
GPlusPerson activity = js.Deserialize<GPlusPerson>(result);

GPlusActivity is a custom class I created that matches its properties with the keys in the JSON structured returned by the Google+ API. Each property in the class must match with the key names in the data structure returned by the API. The JavaScriptSerializer DeSerialize call will convert the data structure in to corresponding .NET object.

In my example, the above API call returns the following JSON data:

JavaScript
{ 
"kind": "plus#person", 
"id": "102428175534077457150", 
"displayName": "Tony John", 
"tagline": "Microsoft MVP, Founder of dotnetspider.com", 
"gender": "male", 
"aboutMe": "", 
"url": "https://plus.google.com/102428175534077457150", 
"image": {  "url": "https://lh3.googleusercontent.com/-wDpbQaFJFKg/
        AAAAAAAAAAI/AAAAAAAAAAA/ZQ8VOHPB1Is/photo.jpg?sz=50" }, 
"urls": [  {   "value": "http://www.thoughtsfromgeeks.com/member/manjaly.aspx"  },  
	   {   "value": "http://www.indiastudychannel.com/member/manjaly.aspx"  },  
	   {   "value": "http://www.dotnetspider.com/member/manjaly.aspx"  } ], 
"organizations": [ {   "name": "LoneStar Informatics LLP",   
                       "title": "Owner",   "type": "work"  } ], 
"placesLived": [  {  "value": "Bangalore, India"  } ]
}

Now, to match the JSON structure, I have defined the .NET class like this:

JavaScript
public class GPlusPerson
{
    public string kind { get; set; }
    public string id { get; set; }
    public string displayName { get; set; }
    public string tagline { get; set; }
    public string birthday { get; set; }
    public string gender { get; set; }
    public string aboutMe { get; set; }
    public string url { get; set; }
    public GImage image { get; set; }
    public Url[] urls { get; set; }
    public Organization[] organizations { get; set; }
    public PlaceLived[] placesLived { get; set; }
}

You can see that each of the keys in the JSON structure has a corresponding property in the .NET class. the DeSerialize method of the JavaScriptSerializer class will map the JSON key-value pair to the corresponding .NET property.

You may notice that some of the items in JSON structure is an array of data by itself. Examples: Image, Organizations, PlacesLived. I have defined each of them as separate types (classes).

You can download the full set of classes to map each of the JSON structure for all the Google+ APIs released in October 2011 (version 1). I have included some samples and basic documentation on how to call each of the Google+ APIs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Tony is a seasoned .NET developer who recently switched his focus to Windows 8 and SEO. You may visit his technology websites: www.dotnetspider.com and www.techulator.com.

Comments and Discussions

 
-- There are no messages in this forum --