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.
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
-- 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)
);
(
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.
Step 2: Add your domain
Microsoft will provide you Client ID and Client secret. Give valid redirect callback URL.
Step 3: Basic Information
Give your application details.
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';
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")");
}
?>
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);
?>
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);
?>
Nice Tut ;-)
ReplyDeleteI 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.
ReplyDeleteThanks for this lovely code.
Hope this will added to your fglogin script ;) too
ReplyDeleteGreat tut ..
Perfect lessons .Thank you.
ReplyDeleteIts really amazing. i liked your post.
ReplyDeleteit mean now you going to add Microsoft Oauth in FG login, FGMLogin
ReplyDeletesuper..thanks
ReplyDeleteNice one !! Can you please give me the link to the twitter OAuth ?!
ReplyDeleteUseful tutorial!!
ReplyDeleteWhy Microsoft Live, this is rather Windows Live isn't?
ReplyDeletethank u. man ;)
ReplyDeletei have to create my own powerful cms portle in php,
ReplyDeletewhat content product software and all i want and how to create this with them.
thanks
what can I add to home.php to get the user's contacts as well?
ReplyDeleteVery Good Post
ReplyDeleteInteresting reading. Perfect lessons!
ReplyDeleteInteresting post, I found some new information. Thanks!
ReplyDeleteGood Tutorial, MS Login functionality is very secure also.
ReplyDeleteWow what a post bro!
ReplyDeleteI am not knowing about this! Thanks for the great information and I will also try it out :D
how can we retrieve contacts list after login please help
ReplyDeletehey 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@Charanjit,
ReplyDeleteCould you send me the error.
this error occur after run your program----------------
ReplyDeleteWe're unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
Hello,
ReplyDeleteCan you please tell me the name of the diagram drawing tool you did above?
Thank you
Hi
ReplyDeleteI 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?
I am using this script and getting following error:
ReplyDeleteWe're unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
What could be the problem?
@Nitish Kr Jha, Anonymous:
ReplyDeleteIf 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!
I have follow all your step but it showing an error that is
ReplyDeleteWe're unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
Please help
i am getting this error after changing appid and secret key
ReplyDeleteError:it was not returned a valid JSON definition of the OAuth access token values
Thanks. It worked for me. great post.
ReplyDeleteThanks. Microsoft recommend registering and managing converged applications by using the new and improved App registrations experience in the Azure Portal. I tried but it does not work with Azure Portal.
ReplyDelete