Login with Microsoft Live OAuth Connect
Wall Script
Follow Me:
Monday, January 21, 2013

Login with Microsoft Live OAuth Connect

Few months back Microsoft has launched OAuth system for client websites, using this you can get the valid user details from Hotmail and Outlook database. Earlier I had published Facebook, Google and Twitter OAuth login systems, now this post explains you how to implement Microsoft Live OAuth connect using PHP. Sure this helps your web project to avoid registration email activation.

Login with Microsoft Live OAuth Connect



Download Script
     Live Demo

The tutorial contains one folder called lib with five PHP files.
lib
-- http.php 
-- oauth_client.php //OAuth client library
index.php
home.php
microsoft_login.php // Microsoft login file
db.php
logout.php

Previous Posts
Google Account OAuth and Facebook and Twitter and
Facebook and Google OAuth System

Database
Sample database users table columns id, email, oauth_uid, oauth_provider and username.
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
full_name VARCHAR(200),
first_name VARCHAR(100),
last_name VARCHAR(100),
email VARCHAR(200),
gender VARCHAR(10),
birthday VARCHAR(20),
provider_id VARCHAR(100)
);

Step 1: Create Microsoft Application
Click here to launch Microsoft live development dashboard and click create application link.
Login with Microsoft Live OAuth Connect

Step 2: Add your domain
Microsoft will provide you Client ID and Client secret. Give valid redirect callback URL.
Login with Microsoft Live OAuth Connect

Step 3: Basic Information
Give your application details.
Login with Microsoft Live OAuth Connect

microsoft_login.php
You have to modify client_id and client_secret values.
$client->redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].
dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/microsoft_login.php';

$client->client_id = 'Microsoft Client ID';
$client->client_secret = 'Microsoft Client Secret';

home.php
Contains PHP code inserting Microsoft OAuth session details into users table.
<?php
session_start();
include('db.php'); //Database Connection.
if (!isset($_SESSION['userdata'])) {
// Redirection to application home page. 
header("location: index.php");
}
else
{
$userdata=$_SESSION['userdata'];
$name =$userdata->name;
$microsoft_id =$userdata->id;
$first_name =$userdata->first_name;
$last_name =$userdata->last_name;
$gender=$userdata->gender;
$email=$userdata->emails->account;
$email2=$userdata->emails->preferred;
$locale=$userdata->locale;
$birth_day=$userdata->birth_day.'-'.$userdata->birth_month.'-'.$userdata->birth_year;
$sql=mysqli_query($db,"insert into users(full_name,first_name,last_name,email,gender,birthday,provider_id) values("$name","$first_name","$last_name","$email","$gender","$birth_day","$microsoft_id")");
}
?>

db.php
Database configuration file, modify username, password and database values.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


31 comments:

  1. I am reading this for first time to have login with microsoft id, In this era when everywhere on web we can see login with Google or FB and adding microsoft/outlook will definitely enhance login experience of users.

    Thanks for this lovely code.

    ReplyDelete
  2. Hope this will added to your fglogin script ;) too
    Great tut ..

    ReplyDelete
  3. Its really amazing. i liked your post.

    ReplyDelete
  4. it mean now you going to add Microsoft Oauth in FG login, FGMLogin

    ReplyDelete
  5. Nice one !! Can you please give me the link to the twitter OAuth ?!

    ReplyDelete
  6. Why Microsoft Live, this is rather Windows Live isn't?

    ReplyDelete
  7. i have to create my own powerful cms portle in php,
    what content product software and all i want and how to create this with them.
    thanks

    ReplyDelete
  8. what can I add to home.php to get the user's contacts as well?

    ReplyDelete
  9. Interesting reading. Perfect lessons!

    ReplyDelete
  10. Interesting post, I found some new information. Thanks!

    ReplyDelete
  11. Good Tutorial, MS Login functionality is very secure also.

    ReplyDelete
  12. Wow what a post bro!
    I am not knowing about this! Thanks for the great information and I will also try it out :D

    ReplyDelete
  13. how can we retrieve contacts list after login please help

    ReplyDelete
  14. hey bro i m using this script i have done all the steps you told but i am unable to login with microsoft i think it might be some error please help me to

    ReplyDelete
  15. @Charanjit,

    Could you send me the error.

    ReplyDelete
  16. this error occur after run your program----------------

    We're unable to complete your request
    Microsoft account is experiencing technical problems. Please try again later.

    ReplyDelete
  17. Hello,
    Can you please tell me the name of the diagram drawing tool you did above?

    Thank you

    ReplyDelete
  18. Hi

    I just logged in with a account with both gender and birthday set, but they returned null. Same with a php script I got from another tutorial.

    What could be the problem?

    ReplyDelete
  19. I am using this script and getting following error:

    We're unable to complete your request
    Microsoft account is experiencing technical problems. Please try again later.

    What could be the problem?

    ReplyDelete
  20. @Nitish Kr Jha, Anonymous:

    If you got the following error:

    We're unable to complete your request
    Microsoft account is experiencing technical problems. Please try again later

    You shoud login Application Management page (https://account.live.com/developers/applications) and edit Redirect URL that is equal with $client->redirect_uri (in your source code).

    It worked fine for me. Hope this will help you!




    ReplyDelete
  21. Nice post, i have got lots of information from here.
    good work. Thanks

    hotmail login problems

    ReplyDelete
  22. I have follow all your step but it showing an error that is
    We're unable to complete your request
    Microsoft account is experiencing technical problems. Please try again later.
    Please help

    ReplyDelete
  23. i am getting this error after changing appid and secret key
    Error:it was not returned a valid JSON definition of the OAuth access token values

    ReplyDelete
  24. Thanks. It worked for me. great post.

    ReplyDelete

Make in India