1. Download Latest ASPSnippets.GoogleAPI DLL from here
2. Get the Google Client ID and Secret. For details refer
Google Developer Console: Generate Client ID and Client Secret for use with Google APIs
3. Use the following code
HTML
<asp:Button ID="btnLogin" Text="Login" runat="server" OnClick="Login" />
<asp:Panel ID="pnlProfile" runat="server" Visible="false">
<hr />
<asp:GridView ID="gvContacts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Email" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ImageUrl='<%# Eval("PhotoUrl") %>' runat="server" onerror="this.src='default.png';" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
Namespaces
using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;
using System.Data;
Code
protected void Page_Load(object sender, EventArgs e)
{
GoogleConnect.ClientId = "<Client ID>";
GoogleConnect.ClientSecret = "<Client Secret>";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Contacts;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
{
string code = Request.QueryString["code"];
string json = GoogleConnect.Fetch("me", code, 500);
GoogleContacts profile = new JavaScriptSerializer().Deserialize<GoogleContacts>(json);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
new DataColumn("Email", typeof(string)),
new DataColumn("PhotoUrl",typeof(string)) });
foreach (Contact contact in profile.Feed.Entry)
{
string name = contact.Title.T;
string email = contact.GdEmail.Find(p => p.Primary).Address;
Link photo = contact.Link.Find(p => p.Rel.EndsWith("#photo"));
string photoUrl = GoogleConnect.GetPhotoUrl(photo != null ? photo.Href : "");
dt.Rows.Add(name, email, GoogleConnect.GetPhotoUrl(photo != null ? photo.Href : ""));
gvContacts.DataSource = dt;
gvContacts.DataBind();
}
pnlProfile.Visible = true;
btnLogin.Enabled = false;
}
if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
}
protected void Login(object sender, EventArgs e)
{
GoogleConnect.Authorize(Server.UrlEncode("https://www.google.com/m8/feeds/"));
}
protected void Clear(object sender, EventArgs e)
{
GoogleConnect.Clear();
}
public class GoogleContacts
{
public Feed Feed { get; set; }
}
public class Feed
{
public GoogleTitle Title { get; set; }
public List<Contact> Entry { get; set; }
}
public class GoogleTitle
{
public string T { get; set; }
}
public class Contact
{
public GoogleTitle Title { get; set; }
public List<Email> GdEmail { get; set; }
public List<Link> Link { get; set; }
}
public class Email
{
public string Address { get; set; }
public bool Primary { get; set; }
}
public class Link
{
public string Rel { get; set; }
public string Type { get; set; }
public string Href { get; set; }
}