Wednesday 13 February 2013


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=mysql_query("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.
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "databasename";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

No comments:

Post a Comment