Importing Contacts from Yahoo Mail using OAuth
Importing contacts from mail accounts using OAuth is a long solved problem which doesn’t have a good implementation. I googled and googled for a good library which imports contacts from Yahoo/GMail/Hotmail. Finally, I was forced to create one by myself. Here are the instructions on how to get it up and running for Yahoo Mail in your server in 2 mins.
1. You need a Yahoo API Key to fetch contacts from Yahoo. Proceed to Yahoo Developer Dashboard and create a key.
Application URL in the createKey page is the URL to which you will be redirected to after a successful/failed login attempt.
Application Domain is be your domain name.
Choose “This app requires access to private user data.” in the Access Scope and under the options which appear below that, select “Read” access for “Yahoo! Contacts”.
Store the API Key, Shared Secret and Application ID carefully.
2. Download Yahoo Social SDK for PHP from http://developer.yahoo.com/social/sdk/php/
$this->config->item(‘yahoo_consumerkey’) is your Yahoo API Key.
$this->config->item(‘yahoo_consumersecret’) is your Yahoo Shared Secret.
$this->config->item(‘yahoo_applicationurl’) is your Application URL you provided in Step 1.
$this->config->item(‘yahoo_applicationid’) is your Yahoo Application ID.
3. Add the following code where the user has to select Yahoo Mail
<a href="<?php echo YahooSession::createAuthorizationUrl($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationurl').'?data=abc'); ?>">Fetch Yahoo Contacts</a>
You may remove the ?data=abc if you don’t want to pass any data to Yahoo. Anything you pass here can retrieved back at Application URL as GET parameters.
4. Include the required files from the opensocial SDK library and then add the following snippet to the Application URL you provided in Step 1.
if (YahooSession::hasSession($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationid')))
{
$session = YahooSession::requireSession($this->config->item('yahoo_consumerkey'), $this->config->item('yahoo_consumersecret'), $this->config->item('yahoo_applicationid'));
$user = $session->getSessionedUser();
$contacts = $user->getContacts(0, 1000);
foreach ($contacts->contacts->contact as $contact)
{
foreach ($contact->fields as $field)
{
if ($field->type == "email")
{
$emails[] = $field->value;
}
}
}
//$_GET['data'] will be equal to "abc" at this page.
}
//$emails array has the email addresses of all the contacts.
5. That’s it folks. It is that simple.
Further reading :
1. http://developer.yahoo.com/social/sdk/php/
2. http://developer.yahoo.com/oauth/













Can you please post the complete source code for this Yahoo contact garbber ?
I can’t post the working code, since I made this for codeigniter. If you need any help, let me know.
Hello! I would like some more details about where to put the files…Yahoo Social SDK in my application. I use Code Igniter, too. Also..do you have a demo of this or a site where you used it?
Thanks
How can i get my contacts from yahoo using yahoo api’s?
I have done upto get accesstoken, rest i dont know what to do, i have failed somany times when i tried contact api. I have some doubts in passing the parameters through GET method to this API. I am doing this in JAVA using play framework.
Hi,
Thank you for your quick tutorial. It is very helpful.
I have a question about Yahoo API. Do you know how can I get Yahoo userID of user, which I can use it as him/her Email address?
$field->value is the user id of the yahoo contact.
Hi dude, first of all thank you… I made as you showed in your tutrial, and it worked, it works, but not all time. I wonder why. Sometimes I retrieve contact list, many times I don’t why? thanx in advance.
Can you send me the source code or post demo
Domain…
[...]sp2hari@weblog… | Importing Contacts from Yahoo Mail using OAuth[...]…
Which files do i need to include and where?