Wednesday 15 March 2017

Login With Yahoo PHP API

Yahoo is one of the network giant.I remember now once upon a time when i was learning class 3rd(2004) I have accessed Yahoo home page from my school internet and and many websites too.btw i Dont know that was internet and Web browser. and This is just a simple post for login with yahoo API for fetching users detail for our web application and authentication.This is basic for fetching users data such as Contacts/Basic Profile and used for posting status and whatever API.We could use general API too. such as Whether forecast,Business Stocks,news headlines.however this is old API familiar to every one,I'm gonna add this and integrate with invite by email script with Google!

Installation : 

Yahoo provides a Library for their Oauth service with many inbuilt functions such as checking session and fetching users data.I have Included it in Demo File.use the Resource.

  • First of all create a developer account and login here to create. 
  • Create New Application[project] for Web Oauth2 Authentication and Fetching Users Data
  • register your app

    • In Scope choose private data -> contacts and ur preferred options for your application

Choose private data -> contacts -> preferred read or write
  • After Final Step is to verify your Domain by uploading a file to root of the website.
  • Get your Application ID, Consumer Key,Consumer Secret and Note your Domain is verified
    and you can add details about your app in Detail section too.
  • final app registered

    Configuration :

    Under index.php just we are adding our Application details and defining ,They are used to authenticate your application and provide you the user data.
    define('OAUTH_DOMAIN', '');
    define('OAUTH_APP_ID', 'APP_ID');

    Authenticate The User :

    Include the Yahoo library and lets make authenticate URL,we just need to pass a callback URL alone which can be defined ourself.And to maintain the Authentication flow A Javascript is introduced for User convenience , A popup for authentication and loading the page once again for API calls and fetching user data.So , We have user in_popup in our Callback url to know that the user has authenticated and to reload the page

    YahooSession is a predefined class with member function to create authurlfor our application by passing our key,secret and callback url

      $callback = "";

      // this URL will be used for the pop-up.

      $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback);

    place the $auth_url in the form of Login with Yahoo link button.

    Checking for Session : 

    There are two type of function to easily manage session in our application.
    1. hasSession method is used to check the web app has previous any session and this is used to determine to login once again with a popup.
    2. requireSession method is to generate a session for us/fetch session existing one and our session data consist of the profile of the user.

    $hasSession = YahooSession::hasSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

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

    Loading user Data and Displaying Details :

    So,by using the above methods we could check the existing session or request to create new session and we could load the user profile data in our web application.

    if($session_check) {
        $user = $session->getSessionedUser();
      // Load the profile for the current user.
        $profile = $user->getProfile();
    try to var_dump the $profile array to see whole data of User who logined in to web application.For full code download and have a try in your Domain.i am leaving in your part for exception handling and checking sessions and showing login button to the users in your application.

    Note : For using Yahoo API , Your domain must be verified[file upload in root directory] and above code runs in popup for authentication purpose ,enable your popups!