Get Google contacts

Although neither POP3 nor IMAP protocol allows retrieving the list of user’s 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.EmailScope, 
        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)() { _
	GoogleScope.EmailScope, _
	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)

Tags: , ,

2 Responses to “Get Google contacts”

  1. amol sonawnae Says:

    I am new to oauth concept. but able to get mails from gmail account by oauth using your library.

    Now as i want to get contacts from Gmail:

    string rawReturnUrl = Console.ReadLine();
    ReturnUrl returnUrl = new ReturnUrl(rawReturnUrl);
    oauth.GetAccessToken(returnUrl.OAuthVerifier);

    I am not getting rawReturnUrl should could contain,from where I will get that url.

    one more thing how can i genrate access token.

    string authUrl = oauth.GetAuthorizationUrl(“http://localhost:64119/”, scopes);

    “http://localhost:64119/” -> what this means…

    from the code below I was able to get url which contains access token but it was not able to
    instataniate googleapi object.
    GoogleApi google = new GoogleApi(“accestoken”);
    gives error unauthorized access.

    can you please help me.

  2. Limilabs support Says:

    @Amol,

    I am not getting rawReturnUrl should could contain,from where I will get that url.

    This is an url to which the browser is redirected by Google.
    You need to extract or copy it from your browser or use code appropriate for ASP.NET.

    one more thing how can i genrate access token.

    You don’t generate access token. It is generated by Google.

    string authUrl = oauth.GetAuthorizationUrl(“http://localhost:64119/”, scopes);
    “http://localhost:64119/” -> what this means…

    This is the address to which Google redirects the browser after successful authorization.

    Also consider using OAuth 2.0 to download contacts from Gmail.

Questions?

Consider using our Q&A forum for asking any questions.