{"id":544,"date":"2010-05-28T14:43:40","date_gmt":"2010-05-28T21:43:40","guid":{"rendered":"http:\/\/sp2hari.com\/?p=544"},"modified":"2020-06-20T08:29:28","modified_gmt":"2020-06-20T08:29:28","slug":"importing-contacts-from-google-mail-using-oauth","status":"publish","type":"post","link":"https:\/\/sp2hari.com\/index.php\/2010\/05\/28\/importing-contacts-from-google-mail-using-oauth\/","title":{"rendered":"Importing Contacts from Google Mail using OAuth"},"content":{"rendered":"<p>This is my second post in the fetch contacts from email account series. In this post, we are going to fetch contacts from a Google account. This isn&#8217;t a detailed tutorial, but a quick 2 minute how-to.<\/p>\n<p>$this-&gt;config-&gt;item(&#8216;google_return_url&#8217;) is the url of the page to which user is returned after a login attempt.<\/p>\n<p>1. Add the following code where the user has to select Google Mail to import contacts.<\/p>\n<p><code>&lt;a href=\"https:\/\/www.google.com\/accounts\/AuthSubRequest?next=&lt;?php echo $this-&gt;config-&gt;item('google_return_url').'?data=abc'; ?&gt;&amp;scope=http:\/\/www.google.com\/m8\/feeds\/contacts\/default\/thin&amp;secure=0&amp;session=1\"&gt;Fetch Google Contacts&lt;\/a&gt;<\/code><\/p>\n<p>2. Include these 2 functions in your PHP code.<br \/>\n<code>function make_api_call($url, $token)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$ch = curl_init();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_URL, $url);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$curlheader[0] = sprintf(\"Authorization: AuthSub token=\\\"%s\\\"\/n\", $token);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$output = curl_exec($ch);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;curl_close($ch);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return $output;<br \/>\n}<br \/>\n&nbsp;<br \/>\nfunction get_session_token($onetimetoken)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$output = make_api_call(\"https:\/\/www.google.com\/accounts\/AuthSubSessionToken\", $onetimetoken);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if (preg_match(\"\/Token=(.*)\/\", $output, $matches))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$sessiontoken = $matches[1];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;} else {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"Error authenticating with Google.\";<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return $sessiontoken;<br \/>\n}<\/code><br \/>\n3. Add the following code to the return url file.<\/p>\n<p><code>$sessiontoken = get_session_token($_GET['token']);<br \/>\n$contacts = make_api_call(\"http:\/\/www.google.com\/m8\/feeds\/contacts\/default\/thin?alt=json&amp;max-results=1000\", $sessiontoken);<br \/>\n$contacts = json_decode($contacts, TRUE);<br \/>\nforeach ($contacts['feed']['entry'] as $contact)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;$emails[] = $contact['gd$email'][0]['address'];<br \/>\n}<br \/>\n\/\/$emails array has the email addresses of all the contacts.<\/code><br \/>\n4. That&#8217;s it folks. It is that simple.<\/p>\n<p>Further reading :<br \/>\n1. <a href=\"http:\/\/code.google.com\/apis\/contacts\/\">http:\/\/code.google.com\/apis\/contacts\/<\/a><br \/>\n2. <a href=\"http:\/\/code.google.com\/apis\/contacts\/docs\/3.0\/reference.html\">http:\/\/code.google.com\/apis\/contacts\/docs\/3.0\/reference.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my second post in the fetch contacts from email account series. In this post, we are going to fetch contacts from a Google account. This isn&#8217;t a detailed tutorial, but a quick 2 minute how-to. $this-&gt;config-&gt;item(&#8216;google_return_url&#8217;) is the url of the page to which user is returned after &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":10,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":1353,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/544\/revisions\/1353"}],"wp:attachment":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}