{"id":3953,"date":"2013-04-10T18:24:10","date_gmt":"2013-04-10T16:24:10","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=3953"},"modified":"2014-11-09T12:08:47","modified_gmt":"2014-11-09T10:08:47","slug":"get-google-contacts-with-oauth2","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/get-google-contacts-with-oauth2","title":{"rendered":"Get Google contacts with OAuth 2.0"},"content":{"rendered":"<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<p>As long as you are using one of OAuth 2.0 scenarios:<\/p>\n<ul>\n<li><a href=\"\/blog\/oauth2-gmail-imap-web-applications\">OAuth 2.0 with Gmail over IMAP for web applications<\/a><\/li>\n<li><a href=\"\/blog\/oauth2-gmail-imap-installed-applications\">OAuth 2.0 with Gmail over IMAP for installed applications<\/a><\/li>\n<li><a href=\"\/blog\/oauth2-gmail-imap-service-account\">OAuth 2.0 with Gmail over IMAP for service account<\/a><\/li>\n<\/ul>\n<p>Mail.dll <a href=\"\/mail\">email component<\/a> allows you to easy download Gmail contacts of a particular user.<\/p>\n<h2>Turn on Contacts API<\/h2>\n<p>Remember to turn on &#8220;Contacts API&#8221; in <a href=\"https:\/\/console.developers.google.com\">Google management console<\/a>.<\/p>\n<p>Remember to add request for calendar data access using <em>GoogleScope.ContactsScope<\/em>.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nList&lt;GoogleScope&gt; scope = new List&lt;GoogleScope&gt;\r\n    {\r\n        GoogleScope.ImapAndSmtp.Name,\r\n        GoogleScope.EmailScope,\r\n        GoogleScope.ContactsScope\r\n    };\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim scope As New List(Of GoogleScope)() { _\r\n    GoogleScope.ImapAndSmtp.Name, _\r\n    GoogleScope.EmailScope, _\r\n    GoogleScope.ContactsScope _\r\n}\r\n\r\n<\/pre>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nGoogleApi api = new GoogleApi(accessToken);\r\n\r\nXmlDocument contacts = api.GetContacts();\r\n\r\nXmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);\r\nnsmgr.AddNamespace(&quot;gd&quot;, &quot;http:\/\/schemas.google.com\/g\/2005&quot;);\r\nnsmgr.AddNamespace(&quot;a&quot;, &quot;http:\/\/www.w3.org\/2005\/Atom&quot;);\r\n\r\nforeach (XmlNode contact in contacts.GetElementsByTagName(&quot;entry&quot;))\r\n{\r\n    XmlNode title = contact.SelectSingleNode(&quot;a:title&quot;, nsmgr);\r\n    XmlNode email = contact.SelectSingleNode(&quot;gd:email&quot;, nsmgr);\r\n\r\n    Console.WriteLine(&quot;{0}: {1}&quot;, \r\n        title.InnerText, \r\n        email.Attributes&#x5B;&quot;address&quot;].Value);\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim api As New GoogleApi(accessToken)\r\n\r\nDim contacts As XmlDocument = api.GetContacts()\r\n\r\nDim nsmgr As New XmlNamespaceManager(contacts.NameTable)\r\nnsmgr.AddNamespace(&quot;gd&quot;, &quot;http:\/\/schemas.google.com\/g\/2005&quot;)\r\nnsmgr.AddNamespace(&quot;a&quot;, &quot;http:\/\/www.w3.org\/2005\/Atom&quot;)\r\n\r\nFor Each contact As XmlNode In contacts.GetElementsByTagName(&quot;entry&quot;)\r\n\tDim title As XmlNode = contact.SelectSingleNode(&quot;a:title&quot;, nsmgr)\r\n\tDim email As XmlNode = contact.SelectSingleNode(&quot;gd:email&quot;, nsmgr)\r\n\tConsole.WriteLine(&quot;{0}: {1}&quot;, _\r\n            title.InnerText, _\r\n            email.Attributes(&quot;address&quot;).Value)\r\nNext\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<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. As long as you are using one of OAuth 2.0 scenarios: OAuth 2.0 with Gmail over IMAP for web applications OAuth 2.0 with Gmail over IMAP for installed applications OAuth 2.0 with Gmail [&hellip;]<\/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,84],"class_list":["post-3953","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-email-component","tag-gmail","tag-oauth-2-0"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3953"}],"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=3953"}],"version-history":[{"count":14,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3953\/revisions"}],"predecessor-version":[{"id":4843,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/3953\/revisions\/4843"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=3953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=3953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=3953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}