Yahoo Oauth Login Connect
Wall Script
Follow Me:
Thursday, January 20, 2011

Yahoo Oauth Login Connect

Nowadays every website authenticating a user using some of the greatest trusty third party websites like Google, yahoo or some of the social networks. Today we are learning how to authenticate a user using his Yahoo account. By using third party login we make user feel happy by avoiding filling a registration form. So just start with a simple illustration.

Yahoo connect using Social sdk

Download Script     Live Demo

About Author
Ravi Tamada
Ravi Tamada
Designer Developer & Freelance
Chennai, INDIA
androidhive.info



The Basic work flow
Yahoo connect using Social sdk

Little Description
1. If user want to login with his yahoo account he is redirected to yahoo login page from our website for authentication.
2. After successful login yahoo issues user data along with his GUID (Globally Unique Identifier) to our website.
3. We are identifying a user using his GUID in our database. In third step we need to check our database for existence of GUID if it is present the user is old user and display his account. If GUID is not present insert a new record and allot new privileges for him by creating a new account.
4. Display user account with his data.

In order to start working with Yahoo SDK you need to register a web application and get the Application ID, Consumer keys.

1. Creating a new Application
a) Register a new application
b) Get the Application ID, Consumer Key and Consumer Secret.

Yahoo connect using Social sdk
Yahoo connect using Social sdk
Yahoo connect using Social sdk
Yahoo connect using Social sdk

After getting the Keys for your application next step is to Create a Table for the users.

2. Designing the Database

Create a table for the users with the fields uid, oauth_vendor, oauth_id, name

CREATE TABLE users(
uid INT(11) PRIMARY KEY AUTO_INCREMENT,
oauth_vendor VARCHAR(15),
oauth_id varchar(50),
name VARCHAR(30)
);

3. User authentication and Storing User GUID

yahoo_connect.php
<?php

// Include the YOS library.
require 'lib/Yahoo.inc';
include 'db_config.php';

session_start();

define('OAUTH_CONSUMER_KEY', 'your_consumer_key'); // Place Yoru Consumer Key here
define('OAUTH_CONSUMER_SECRET', 'your_consumer_secret'); // Place your Consumer Secret
define('OAUTH_APP_ID', 'your_app_id'); // Place Your App ID here

// If user clicks on LOGIN button
if (array_key_exists("login", $_GET))
{
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if (is_object($session))
{
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID

//Retriving the user
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
$result = mysql_fetch_array($query);

if (empty($result))
{
// user not present in Database. Store a new user and Create new account for him
$query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')");
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'");
$result = mysql_fetch_array($query);
}

// Creating session variable for User
$_SESSION['login'] = true;
$_SESSION['name'] = $result['name'];
$_SESSION['guid'] = $result['guid'];
$_SESSION['oauth_provider'] = 'yahoo';
}
}

// If user clicks on LOGOUT button
if (array_key_exists("logout", $_GET)) {
// User logging out and Clearing all Session data
YahooSession::clearSession();
unset ($_SESSION['login']);
unset($_SESSION['name']);
unset($_SESSION['guid']);
unset($_SESSION['oauth_provider']); // After logout Redirection here
header("Location: index.php");
}
?>

Login Page

login.php
<?php
include 'yahoo_connect.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Yahoo Authentication</title>
</head>
<body>

<?php
if ($_SESSION['login'] == true)
{
echo '<br/><a href="?logout"><img src="images/logout_btn.png" alt="Yahoo Logout"/></a>';
}
else
{
echo '<a href="?login"><img src="images/login_btn.png" alt="Yahoo Login"/></a>';
}
?>

</body>
</html>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


92 comments:

  1. nice tutorial, always helpful!

    ReplyDelete
  2. Great tutorial, thanks for the informative video and images... very helpful ! 5 stars

    ReplyDelete
  3. Thanks for sharing that work flow chart.. Nice..

    ReplyDelete
  4. @dskanth : Thank You for your 5 STARS :)

    ReplyDelete
  5. NO NO NO NO NO NO
    ---------------
    i am dreaming
    u r amizing
    thank u * 9999999999999
    u are help me
    ^^
    u still my bro
    god help u

    ReplyDelete
  6. Nice tuts, I hope you can make a google auth

    ReplyDelete
  7. thanks for sharring Ravi (very helpful), what if the google / gmail oauth login connect :) we wait for the next tutorial, friend :)

    ReplyDelete
  8. amazing amazing bos...good good

    ReplyDelete
  9. interesting article on oauth login connect

    ReplyDelete
  10. thanks.. i couldnt understand after step 3 cuz im an asp.net programer. anyway i got the idea

    ReplyDelete
  11. yahoo openid is not working why?.if i click loginlink it is displayed like this not redirect to yahoo api page http://xxxxxx.com?login

    ReplyDelete
  12. when i click login button there is no action happened.there is no redirection to yahoo api login page.i gave secret,consumer and api id.but i don't know why it is not working.will it run on localhost. i think that i don'
    t know about how to run this application.please reply me
    url:http://www.9lessons.info/2011/01/yahoo-oauth-login-connect.html

    ReplyDelete
  13. same here. nothing happens when we click login. it doesnt redirects to yahoo

    ReplyDelete
  14. Is it possible to retrieve the email from this API?

    ReplyDelete
  15. can we get the unread mails from yahoo. if yes can you share code for getting mails

    ReplyDelete
  16. is there any way to get the yahoo unread mails, if yes please share the code

    ReplyDelete
  17. hello can some one help ..its not redirecting ..

    ReplyDelete
  18. I am facing the problem of oauth_problem=consumer_key_rejected

    any reason?

    ReplyDelete
  19. Fix error 'oauth_problem=consumer_key_rejected'

    http://till.klampaeckel.de/blog/archives/153-Yahoo-oauth_problemconsumer_key_rejected.html

    ReplyDelete
  20. hi i am unable to sue this script can you guide me up usually i have setup the whole script as per guidance still there is no effect when i click on login button pleas tdo help

    ReplyDelete
  21. Nothing happen.. the last url only show ?login
    what happend ? please help me.. it supposed to direct me to yahoo right ?

    ReplyDelete
  22. hi iam getting error
    Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\yahoo\lib\Yahoo.inc on line 1810
    like this .....
    can any help me

    ReplyDelete
  23. Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\yahoo\lib\Yahoo.inc on line 1810


    hiany one help me i am getting a problem when clicking on yahoologin button

    ReplyDelete
  24. The question of no redirect after clicking on LOGIN is asked so many times here and still no answer. What do we do please?

    ReplyDelete
  25. sorry mate, doesnt work...
    $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

    doesnt return nything

    ReplyDelete
  26. hi i am getting a problem in login with yahoo..
    i am using code--
    $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
    if (is_object($session)) {
    $user = $session->getSessionedUser();
    $profile = $user->getProfile();
    $name = $profile->nickname; // Getting user name
    $guid = $profile->guid; // Getting Yahoo ID
    }
    but when i click on yahoo login button page remain same. i also define OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID.
    but if i do:--
    if (!is_object($session)) {echo "djf"; }
    its working ..plz help me

    ReplyDelete
  27. hi i am getting a problem in login with yahoo..
    i am using code--
    $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
    if (is_object($session)) {
    $user = $session->getSessionedUser();
    $profile = $user->getProfile();
    $name = $profile->nickname; // Getting user name
    $guid = $profile->guid; // Getting Yahoo ID
    }
    but when i click on yahoo login button page remain same. i also define OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID.
    but if i do:--
    if (!is_object($session)) {echo "djf"; }
    its working ..plz help me

    ReplyDelete
  28. The question of no redirect after clicking on LOGIN is asked so many times here and still no answer. What do we do please?

    ReplyDelete
  29. hi, it's isn't working, it's said that

    Strict Standards: Non-static method YahooSession::requireSession() should not be called statically in ../yahoo_connect.php on line 14

    can you tell me, is it ok to change the method to static method ? or maybe you had another solutions, please, response me asap

    ReplyDelete
  30. In Api after getting the Keys for your application check for SUB DIRECTORY option in the list provided below to your keys.

    Select the RADIO BUTTON Read/Write Public and Private inorder to get login page..

    You won't be redirected to login page and will be in same page until you select this radui button and save the information.

    ReplyDelete
  31. After getting API Keys,

    You need to CHeck for SOCIAL DIRECTORY option below keys.

    You need to check " Read/Write Public and Private" RADIO BUTTON.

    You remain in the same page and won't be redirected to login page until u select this radio button and save the information.

    ReplyDelete
  32. It does not work in my site!

    ReplyDelete
  33. can we do the above complete implementation in javascript

    ReplyDelete
  34. Nishanth: wer to create table for database?

    ReplyDelete
  35. $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);


    is not working..

    ReplyDelete
  36. nice but i don't have lib/Yahoo.inc

    ReplyDelete
  37. Good Job

    Can You please share me how to get group and friendlist for linkedin in php

    ReplyDelete
  38. $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);


    is not working..

    plzzz help

    ReplyDelete
  39. I hope you can make a tuts about get yahoo contact list. Thanks!

    ReplyDelete
  40. hello i got following warning, it working but when another user gets logged in than it goest provide name and guid.... what should i do any clue??
    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0

    ReplyDelete
  41. it works successfully but sessions are not destroyed properlly.. when new user logged in then it doesn't show name and guid

    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0

    ReplyDelete
  42. Why it should need a live server (domain). can't we do it in a localhost or subdomain

    ReplyDelete
  43. I am looking to integration the login apps of yahoo, gmail, facebook, twitter and msnlive. Can u help me to put all of them together

    ReplyDelete
  44. I downloaded the package and changed the keys and db settings. when i run the script it is not happening. Initially it is not logging in. why?

    ReplyDelete
  45. I found the solution why it is not working. In the file 'lib/yahoo.inc' change the callback url to the verified domain url. then it will works. change the callbackurl in the function 'createAuthorizationUrl'

    ReplyDelete
  46. Hi, no action when I click Login Button, can you help me ?

    ReplyDelete
  47. very nice but how to get user emai

    ReplyDelete
  48. Hi!

    It seems that if you arrive on yahoo registrayion / login page and click "Back" button, the next attempt to login with yahoo throws this error:

    Warning: First parameter must either be an object or the name of an existing class in /home/hotpens/www/yahoo/Yahoo.inc on line 1372

    Warning: First parameter must either be an object or the name of an existing class in /home/domain/www/yahoo/Yahoo.inc on line 1372

    Warning: Cannot modify header information - headers already sent by (output started at /home/domain/www/yahoo/Yahoo.inc:1372) in /home/domain/www/yahoo/Yahoo.inc on line 343

    Any idea on how can it be fixed? Also, it throws the same error if you use a linkedin script and not get logged in and after the yahoo one.

    Thanks

    Awesome login scripts!

    ReplyDelete
  49. Please give example for the same in java/javascript

    ReplyDelete
  50. you showing something else here and script not working what the hell is this ...

    ReplyDelete
  51. Hi, the demo is awesome.. But I am not able download the script. I subscribed and confirmation is also done, but during download it says email does not exist..

    ReplyDelete
  52. Hey Sri,

    $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

    //""" I could not get any $session Value here so it stops here... plz suggest anything """
    if (is_object($session)) {

    $user = $session->getSessionedUser();
    $profile = $user->getProfile();
    $name = $profile->nickname; // Getting user name
    $guid = $profile->guid; // Getting Yahoo ID

    //Retriving the user
    /* $query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
    $result = mysql_fetch_array($query);

    if (empty($result)) {
    // user not present in Database. Store a new user and Create new account for him
    $query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')") or die (mysql_error());
    $query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
    $result = mysql_fetch_array($query);
    }*/
    // Creating session variable for User
    $_SESSION['login'] = true;
    $_SESSION['name'] = $result['name'];
    $_SESSION['guid'] = $result['guid'];
    $_SESSION['oauth_provider'] = 'yahoo';
    }
    else
    {
    echo($session);
    echo("you--------- are here");
    die;
    }
    }

    ReplyDelete
  53. $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

    it is returning null ..Please help

    ReplyDelete
  54. Hi All,

    After configured the download script. I have added some require information in yahoo_connect.php( for key) and db_config.php( fro database) file but when we run the script we are getting below error.

    Strict Standards: Non-static method YahooSession::requireSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/yahoo_connect.php on line 17

    Strict Standards: Non-static method YahooSession::initSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 437

    Strict Standards: Non-static method YahooSession::checkSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 555

    Strict Standards: Non-static method YahooSession::redirectForAuthorization() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 561

    Strict Standards: Non-static method YahooSession::createAuthorizationUrl() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 340

    Strict Standards: Non-static method YahooUtil::current_url() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 474

    Strict Standards: Non-static method YahooAuthorization::getRequestToken() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 478

    Strict Standards: Non-static method YahooLogger::debug() should not be called statically, assuming $this from incompatible context in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 1866

    Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 1322

    Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 489

    Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 348

    Notice: Undefined index: login in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/index.php on line 16

    ReplyDelete
  55. $session = YahooSession::requireSession(CONSUMER_KEY,CONSUMER_SECRET,APPID);

    return Null

    Please check it and tell it for me

    ReplyDelete
  56. YahooSession::requireSession

    it is returning null ..
    Click login button , not redirect

    Please help me !

    ReplyDelete
  57. great job.Can you Please Update to get yahoo! contacts (Like invite your friends in facebook).

    ReplyDelete
  58. I'm getting error: "Fatal error: Call to undefined function curl_init() in F:\Software\wamp\www\test\Yahoo_Oauth_YOS\Yahoo_Oauth_YOS\lib\Yahoo.inc "

    ReplyDelete
  59. Hai! I have some problem with your script:
    1. when test it in localhost it no return nothing when i click button login i see my URL add (..my root url..)?login with errors undefined index in Yahoo_Oauth_YOS\index.php on line 17.
    2.I am try test it on server but after i pushing this script to my server it's make all my code is not run. my index page nothing return something on browser .
    3. if this script is run well how i can get contact list more .
    please help me to show my problem. by the way i note all command you not reply i think it's may be your site no function reply command so you can help me some idea by mail : sok.layheng@yahoo.com

    ReplyDelete
  60. HI... how to get YM chat log using this project.
    thanks before :)

    ReplyDelete
  61. i mean i want to save YM chat into database(myql) :)

    ReplyDelete
  62. This does not provide user's email address. Did you try to get the login email address?

    ReplyDelete
  63. $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);



    its not working plz help me........................

    ReplyDelete
  64. would love a tutorial on getting contact list from yahoo accounts!

    ReplyDelete
  65. thanks for you script it is very helpful

    ReplyDelete
  66. Strict Standards: Non-static method YahooSession::requireSession() should not be called statically in ... How can i fix this bug

    ReplyDelete
  67. thnks.. want some code to retrieve contacts from yahoo to send invitation to my website.. can any1 help me???

    ReplyDelete
  68. Strict Standards: Non-static method YahooSession::requireSession() should not be called statically in ... How can i fix this bug

    ReplyDelete
  69. Strict Standards: Non-static method YahooSession::redirectForAuthorization() should not be called statically in D:\xampp\htdocs\loginyahoo\lib\Yahoo.inc on line 562

    Notice: Undefined index: login in D:\xampp\htdocs\loginyahoo\index.php on line 16

    I'm facing this problem please give some solution

    ReplyDelete
  70. thank you nice script but $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); return null please any help me why problem ......

    ReplyDelete
  71. I have the same problem $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); returns null.
    How can i fix this?

    ReplyDelete
  72. about problem $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); returns null:
    i tick to Social Directory/Read Public - With your user's permission, you can read their Profile information.... on my yahoo API then it work. Hope that help you :)

    ReplyDelete
  73. just put isset($requestToken->sessionHandle) instead of property_exists($requestToken, "sessionHandle") for the issue :

    Warning: First parameter must either be an object or the name of an existing class in /home/hotpens/www/yahoo/Yahoo.inc on line 1372

    Warning: First parameter must either be an object or the name of an existing class in /home/domain/www/yahoo/Yahoo.inc on line 1372

    Warning: Cannot modify header information - headers already sent by (output started at /home/domain/www/yahoo/Yahoo.inc:1372) in /home/domain/www/yahoo/Yahoo.inc on line 343

    I have done it ...

    ReplyDelete
  74. It is really nice.
    Thanks for great job.
    But I am unable to get user email address.
    Can anyone please help me?

    ReplyDelete
  75. Login : http://developer.apps.yahoo.com/projects/
    edit your projects

    Check Permissions for that projects

    ReplyDelete
  76. Login and Edit your project on Yahoo API.
    Check Permission in bottom

    ReplyDelete
  77. Does this still work? I am getting 401 "Custom port is not allowed or the host is not registered with this consumer key." error with sample code... i have checked my consumer keys and other stuff several times...

    ReplyDelete
  78. i seriously wanna cry right now!! at yahoo developer screen i entered my website with www. prefix and on my browser i was writing my website without it www. prefix and that was why it didnt work!! This is ridiculous, they should have allowed it..

    ReplyDelete
  79. redirecting to same page when i click login and getting this error Notice: Undefined index: login in /home2/

    ReplyDelete
  80. hii i'm not getting the Name and Email Address after login into..

    ReplyDelete
  81. Since i am a beginner i dont know how to Link or Connect the above given sdk folder to my login page . so i kindly request you peoples to please guide me....

    ReplyDelete
  82. thank you nice script but $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); return null please any help me why problem ......

    ReplyDelete
  83. This code not working perfectly...plz somebody help..

    ReplyDelete
  84. When i click the login button it redirects nothing!...plz give proper datatbase tables details....

    ReplyDelete
  85. Hi,
    I couldn't able to see the images ... can anyone post it again! Thanks in advance.

    ReplyDelete

Make in India