Get Google contacts
Friday, April 13th, 2012Although neither POP3 nor IMAP protocol allows retrieving the list of users contacts, it is possible to use Google API for that.
As long as you are using OAuth to access Gmail, Mail.dll email component allows you to easy download Gmail contacts of a particular user.
// C#
GmailOAuth oauth = new GmailOAuth(_consumerKey, _consumerSecret);
List<GoogleScope> scopes = new List<GoogleScope>
{
GoogleScope.EmailAddressScope,
GoogleScope.ContactsScope
};
string authUrl = oauth.GetAuthorizationUrl("http://localhost:64119/", scopes);
// ASP.NET client:
// Save oauth in permanent storage:
// Cache[oauth.RequestToken.Token] = oauth;
// Windows client:
Process.Start(url);
// ASP.NET client:
// Response.Redirect(url);
// Windows client with url:
string rawReturnUrl = Console.ReadLine();
ReturnUrl returnUrl = new ReturnUrl(rawReturnUrl);
oauth.GetAccessToken(returnUrl.OAuthVerifier);
// Windows client with verification code (oob):
// string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());
// oauth.GetAccessToken(oauthVerifier);
// ASP.NET client:
// ReturnUrl returnUrl = new ReturnUrl(Request.RawUrl);
// Retrieve oauth from permanent storage:
// GmailOAuth oauth = Cache[returnUrl.OAuthToken]
// oauth.GetAccessToken(returnUrl.OAuthVerifier);
GoogleApi google = new GoogleApi(oauth.ConsumerToken, oauth.AccessToken);
XmlDocument contacts = google.GetContacts();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);
nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
{
XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);
Console.WriteLine("{0}: {1}",
title.InnerText,
email.Attributes["address"].Value);
}
oauth.RevokeToken(oauth.AccessToken.Token);
' VB.NET
Dim oauth As New GmailOAuth(_consumerKey, _consumerSecret)
Dim scopes As New List(Of GoogleScope)() With { _
GoogleScope.EmailAddressScope, _
GoogleScope.ContactsScope _
}
Dim authUrl As String = oauth.GetAuthorizationUrl("http://localhost:64119/", scopes)
' ASP.NET client:
' Save oauth in permanent storage:
' Cache[oauth.RequestToken.Token] = oauth;
' Windows client:
Process.Start(url)
' ASP.NET client:
' Response.Redirect(url)
' Windows client with url:
Dim rawReturnUrl As String = Console.ReadLine()
Dim returnUrl As New ReturnUrl(rawReturnUrl)
oauth.GetAccessToken(returnUrl.OAuthVerifier)
' Windows client with verification code (oob):
' Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())
' oauth.GetAccessToken(oauthVerifier)
' ASP.NET client:
' Dim returnUrl As New ReturnUrl(Request.RawUrl)
' Retrive oauth from permanent storage:
' Dim oauth As GmailOAuth = Cache(returnUrl.OAuthToken)
' oauth.GetAccessToken(returnUrl.OAuthVerifier)
Dim google As New GoogleApi(oauth.ConsumerToken, oauth.AccessToken)
Dim contacts As XmlDocument = google.GetContacts()
Dim nsmgr As New XmlNamespaceManager(contacts.NameTable)
nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005")
nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom")
For Each contact As XmlNode In contacts.GetElementsByTagName("entry")
Dim title As XmlNode = contact.SelectSingleNode("a:title", nsmgr)
Dim email As XmlNode = contact.SelectSingleNode("gd:email", nsmgr)
Console.WriteLine("{0}: {1}", _
title.InnerText, _
email.Attributes("address").Value)
Next
oauth.RevokeToken(oauth.AccessToken.Token)