Login with Yahoo Oauth in php

 

Now a days,websites are needs third party authentication for their login system.Here,We used Yahoo Oauth for login in php using Yahoo PHP SDK. Here, for authentication login we used popup.
Before starting with the Yahoo PHP SDK, you will need to register a web application and you will get web-based application ID,OAuth consumer key and secret. These keys will provide you with general access to the Yahoo! Social Platform, and allow your users to authorize your application to access protected resources and information.

Live DemoLive Demo

Download Script

For Creating the Yahoo App,Please Click Here.

In Yahoo My projects page,Click on the New Project Button.It will appears a popup.In that popup,select the Standard option.

Yahoo Application Type

In that Popup click on Continue button after selection of the Yahoo Application type,you will get the Application form.

Yahoo Application Form

After filling the all fields,Click on the Get API Key.You will get application ID,OAuth consumer key and secret.

Yahoo Authentication Information

After that you need to set the permissions for this app.In the Permissions,You need to select the Read Public option under Social Directory (Profiles) of the Social Directory and after you need to click on Save and Change Consumer Key.

Yahoo Application Permissions

Here is the index.php file.In that file you need to define the OAUTH_CONSUMER_KEY,  OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID

<?php
// Include the YOS library.
require dirname(__FILE__).'/lib/Yahoo.inc';
// debug settings
//error_reporting(E_ALL | E_NOTICE); # do not show notices as library is php4 compatable
//ini_set('display_errors', true);
YahooLogger::setDebug(true);
YahooLogger::setDebugDestination('LOG');
// use memcache to store oauth credentials via php native sessions
session_start();
// Make sure you obtain application keys before continuing by visiting:
// https://developer.yahoo.com/dashboard/createKey.html
define('OAUTH_CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('OAUTH_CONSUMER_SECRET', 'YOUR CONSUMER SECRET');
define('OAUTH_DOMAIN', 'YOUR DOMAIN');
define('OAUTH_APP_ID', 'YOUR APP ID');
if(array_key_exists("logout", $_GET)) {
  // if a session exists and the logout flag is detected
  // clear the session tokens and reload the page.
  YahooSession::clearSession();
  header("Location: index.php");
}
// check for the existance of a session.
// this will determine if we need to show a pop-up and fetch the auth url,
// or fetch the user's social data.
$hasSession = YahooSession::hasSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if($hasSession == FALSE) {
  // create the callback url,
  $callback = YahooUtil::current_url()."?in_popup";
$sessionStore = new NativeSessionStore();
  // pass the credentials to get an auth url.
  // this URL will be used for the pop-up.
  $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback, $sessionStore);

}
else {
  // pass the credentials to initiate a session
  $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
  // if the in_popup flag is detected,
  // the pop-up has loaded the callback_url and we can close this window.
  if(array_key_exists("in_popup", $_GET)) {
    close_popup();
    exit;
  }
  // if a session is initialized, fetch the user's profile information
  if($session) {
    // Get the currently sessioned user.
    $user = $session->getSessionedUser();
    // Load the profile for the current user.
    $profile = $user->getProfile();

  }
}
/**
 * Helper method to close the pop-up window via javascript.
 */
function close_popup() {
?>
<script type="text/javascript">
  window.close();
</script>
<?php
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <title>Login with Yahoo account-Idiot Minds</title>
    <!-- Combo-handled YUI JS files: -->
    <script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="popupmanager.js"></script>
 <!-- Combo-handled YUI CSS files: -->
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/reset-fonts-grids/reset-fonts-grids.css&2.7.0/build/base/base-min.css">

  </head>
  <body>
    <?php
      if($hasSession == FALSE) { 
        // if a session does not exist, output the
        // login / share button linked to the auth_url.
        ?>
       <a href="<?php echo $auth_url; ?>" id="yloginLink"><img src="yahoo-oauth-connect.png" style="width:150px;float:left;margin-left:550px;" /></a>
    <?php  }
      else if($hasSession && $profile) {
        // if a session does exist and the profile data was
        // fetched without error, print out a simple usercard.
        echo sprintf("<img src=\"%s\"/><p><h2>Hi <a href=\"%s\" target=\"_blank\">%s!</a></h2></p>\n", $profile->image->imageUrl, $profile->profileUrl, $profile->nickname);
        if($profile->status->message != "") {
          $statusDate = date('F j, y, g:i a', strtotime($profile->status->lastStatusModified));
          echo sprintf("<p><strong>&#8220;</strong>%s<strong>&#8221;</strong> on %s</p>", $profile->status->message, $statusDate);
        }
        echo "<p><a href=\"?logout\">Logout</a></p>";
      }
    ?>

    <script type="text/javascript">
      var Event = YAHOO.util.Event;
      var _gel = function(el) {return document.getElementById(el)};
      function handleDOMReady() {
        if(_gel("yloginLink")) {
          Event.addListener("yloginLink", "click", handleLoginClick);
        }
      }
      function handleLoginClick(event) {
        // block the url from opening like normal
        Event.preventDefault(event);
        // open pop-up using the auth_url
        var auth_url = _gel("yloginLink").href;
        PopupManager.open(auth_url,600,435);
      }
      Event.onDOMReady(handleDOMReady);
    </script>
  </body>
</html>

62 Comments

  1. nice post…thank you very much

  2. hey.on clicking the yahoo logo im coming back to same page and not redirected to yahoo authentication login. :(

    • In your Yahoo app,Under Permissions of the app,You need to select the Read Public option under Social Directory (Profiles) of the Social Directory and after you need to click on Save and Change Consumer Key.

      And also change the Consumer key and Consumer secret in index.php file

  3. nice post……thnk u

  4. On clicking the yahoo logo image its coming back to same page and not redirected to yahoo authentication login.

  5. Warning: First parameter must either be an object or the name of an existing class in lib/Yahoo.inc on line 1373

    • This warning is due to pass the proper app authentication details.Please make sure that,put the OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID values of your yahoo app

  6. after login not getting any value in
    $profile = $user->getProfile();

    i have done everything as u said in ur article.
    error log say oauth_problem=”unable_to_determine_oauth_type”, realm=”yahooapis.com”

    • you need to set the permissions for this app.In the Permissions,You need to select the Read Public option under Social Directory (Profiles) of the Social Directory and after you need to click on Save and Change Consumer Key.After that You need to define the OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID in index.php file.

  7. Please help,

    I’ve tried many yahoo php sdks and all show this same error:
    Strict Standards: Non-static method YahooLogger::setDebug() should not be called statically in C:\xampp\htdocs\yahoo\index.php on line 12

    many errors like this about 10 are displayed and nothing work then onwards….

  8. Helpful, thanks!

  9. santhosh

    it shows

    Domain verification failed. Please check that the verification file is in the location above and is named correctly.

    this type of error so redirection not working properly

Leave a Reply