{"id":2636,"date":"2012-04-13T16:30:44","date_gmt":"2012-04-13T14:30:44","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=2636"},"modified":"2022-05-04T10:43:30","modified_gmt":"2022-05-04T08:43:30","slug":"get-google-contacts","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/get-google-contacts","title":{"rendered":"Get Google contacts"},"content":{"rendered":"\n<p>Consider using OAuth 2.0:<br><a href=\"https:\/\/www.limilabs.com\/blog\/get-google-contacts-with-oauth2\">https:\/\/www.limilabs.com\/blog\/get-google-contacts-with-oauth2<\/a><\/p>\n\n\n\n<p>Although neither POP3 nor IMAP protocol allows retrieving the list of user&#8217;s contacts, it is possible to use Google API for that.<\/p>\n\n\n\n<p>As long as you are using <a href=\"\/blog\/oauth-with-gmail\">OAuth to access Gmail<\/a>, Mail.dll <a href=\"\/mail\">email component<\/a> allows you to easy download Gmail contacts of a particular user.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nGmailOAuth oauth = new GmailOAuth(_consumerKey, _consumerSecret);\n\nList&lt;GoogleScope&gt; scopes = new List&lt;GoogleScope&gt;;\n    {\n        GoogleScope.EmailScope,\n        GoogleScope.ContactsScope\n    };\n\nstring authUrl = oauth.GetAuthorizationUrl(&quot;http:\/\/localhost:64119\/&quot;, scopes);\n\n\/\/ ASP.NET client:\n\/\/ Save oauth in permanent storage:\n\/\/ Cache&#x5B;oauth.RequestToken.Token] = oauth;\n\n\/\/ Windows client:\nProcess.Start(url);\n\n\/\/ ASP.NET client:\n\/\/ Response.Redirect(url);\n\n\/\/ Windows client with url:\nstring rawReturnUrl = Console.ReadLine();\nReturnUrl returnUrl = new ReturnUrl(rawReturnUrl);\noauth.GetAccessToken(returnUrl.OAuthVerifier);\n\n\/\/ Windows client with verification code (oob):\n\/\/ string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());\n\/\/ oauth.GetAccessToken(oauthVerifier);\n\n\/\/ ASP.NET client:\n\/\/ ReturnUrl returnUrl = new ReturnUrl(Request.RawUrl);\n\/\/ Retrieve oauth from permanent storage:\n\/\/ GmailOAuth oauth = Cache&#x5B;returnUrl.OAuthToken]\n\/\/ oauth.GetAccessToken(returnUrl.OAuthVerifier);\n\nGoogleApi google = new GoogleApi(oauth.ConsumerToken, oauth.AccessToken);\n\nXmlDocument contacts = google.GetContacts();\n\nXmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);\nnsmgr.AddNamespace(&quot;gd&quot;, &quot;http:\/\/schemas.google.com\/g\/2005&quot;);\nnsmgr.AddNamespace(&quot;a&quot;, &quot;http:\/\/www.w3.org\/2005\/Atom&quot;);\n\nforeach (XmlNode contact in contacts.GetElementsByTagName(&quot;entry&quot;))\n{\n    XmlNode title = contact.SelectSingleNode(&quot;a:title&quot;, nsmgr);\n    XmlNode email = contact.SelectSingleNode(&quot;gd:email&quot;, nsmgr);\n\n    Console.WriteLine(&quot;{0}: {1}&quot;,\n        title.InnerText,\n        email.Attributes&#x5B;&quot;address&quot;].Value);\n}\n\noauth.RevokeToken(oauth.AccessToken.Token);\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET\n\nDim oauth As New GmailOAuth(_consumerKey, _consumerSecret)\n\nDim scopes As New List(Of GoogleScope)() { _\n\tGoogleScope.EmailScope, _\n\tGoogleScope.ContactsScope _\n}\nDim authUrl As String = oauth.GetAuthorizationUrl(\"http:\/\/localhost:64119\/\", scopes)\n\n' ASP.NET client:\n' Save oauth in permanent storage:\n' Cache&#x5B;oauth.RequestToken.Token] = oauth;\n\n' Windows client:\nProcess.Start(url)\n\n' ASP.NET client:\n' Response.Redirect(url)\n\n' Windows client with url:\nDim rawReturnUrl As String = Console.ReadLine()\nDim returnUrl As New ReturnUrl(rawReturnUrl)\noauth.GetAccessToken(returnUrl.OAuthVerifier)\n\n' Windows client with verification code (oob):\n' Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())\n' oauth.GetAccessToken(oauthVerifier)\n\n' ASP.NET client:\n' Dim returnUrl As New ReturnUrl(Request.RawUrl)\n' Retrive oauth from permanent storage:\n' Dim oauth As GmailOAuth = Cache(returnUrl.OAuthToken)\n' oauth.GetAccessToken(returnUrl.OAuthVerifier)\n\nDim google As New GoogleApi(oauth.ConsumerToken, oauth.AccessToken)\nDim contacts As XmlDocument = google.GetContacts()\n\nDim nsmgr As New XmlNamespaceManager(contacts.NameTable)\nnsmgr.AddNamespace(\"gd\", \"http:\/\/schemas.google.com\/g\/2005\")\nnsmgr.AddNamespace(\"a\", \"http:\/\/www.w3.org\/2005\/Atom\")\n\nFor Each contact As XmlNode In contacts.GetElementsByTagName(\"entry\")\n\tDim title As XmlNode = contact.SelectSingleNode(\"a:title\", nsmgr)\n\tDim email As XmlNode = contact.SelectSingleNode(\"gd:email\", nsmgr)\n\tConsole.WriteLine(\"{0}: {1}\", _\n            title.InnerText, _\n            email.Attributes(\"address\").Value)\nNext\n\noauth.RevokeToken(oauth.AccessToken.Token)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Consider using OAuth 2.0:https:\/\/www.limilabs.com\/blog\/get-google-contacts-with-oauth2 Although neither POP3 nor IMAP protocol allows retrieving the list of user&#8217;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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[33,25,41],"class_list":["post-2636","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-email-component","tag-gmail","tag-oauth"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2636"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=2636"}],"version-history":[{"count":14,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2636\/revisions"}],"predecessor-version":[{"id":6115,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/2636\/revisions\/6115"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=2636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=2636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=2636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}