Importing Contacts from Google Mail using OAuth

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’t a detailed tutorial, but a quick 2 minute how-to.

$this->config->item(‘google_return_url’) is the url of the page to which user is returned after a login attempt.

1. Add the following code where the user has to select Google Mail to import contacts.

<a href="https://www.google.com/accounts/AuthSubRequest?next=<?php echo $this->config->item('google_return_url').'?data=abc'; ?>&scope=http://www.google.com/m8/feeds/contacts/default/thin&secure=0&session=1">Fetch Google Contacts</a>

2. Include these 2 functions in your PHP code.
function make_api_call($url, $token)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $token);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheader);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
 
function get_session_token($onetimetoken)
{
    $output = make_api_call("https://www.google.com/accounts/AuthSubSessionToken", $onetimetoken);
    if (preg_match("/Token=(.*)/", $output, $matches))
    {
        $sessiontoken = $matches[1];
    } else {
        echo "Error authenticating with Google.";
        exit;
    }
    return $sessiontoken;
}

3. Add the following code to the return url file.

$sessiontoken = get_session_token($_GET['token']);
$contacts = make_api_call("http://www.google.com/m8/feeds/contacts/default/thin?alt=json&max-results=1000", $sessiontoken);
$contacts = json_decode($contacts, TRUE);
foreach ($contacts['feed']['entry'] as $contact)
{
    $emails[] = $contact['gd$email'][0]['address'];
}
//$emails array has the email addresses of all the contacts.

4. That’s it folks. It is that simple.

Further reading :
1. http://code.google.com/apis/contacts/
2. http://code.google.com/apis/contacts/docs/3.0/reference.html

Google Image Search

I don’t remember who exactly gave me this script. Must be either Verma or Manas, but this one is so cool.
The code looks pretty easy and clear, though I didn’t try to understand it completely.
So this is what you have to do.

Open Google image search and search for some image. If you are lazy to do that, click
http://images.google.com/images?hl=en&q=sp2hari&btnG=Search+Images&gbv=2

Then copy this javascript code and paste it in the URL bar
javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI= document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5); void(0)

Cool right? ;-)

Topping google search

I attended the podworks.in last weekend. For those who don’t know what podworks is , PodWorks.in is a 2-day workshop styled unconference dedicated to the sharing of knowledge around audio and video podcasting. It’s a successor to the extremely successful BlogCamp that was held last year.

Though i have lots and lots to say about the event, one strange and nice thing is that if you google for podworks.in my blog tops the list :-) :-)