Login System with Windows/Microsoft Live using OAuth php and mysql

Login System with Windows Microsoft Live using OAuth php and mysql

Microsoft/Windows Live also gives to the ability to make a social login app. It is very simple to quick to integrate. In this tutorial I will explain how to create a Microsoft/Windows application for web, and also to integrate a login system with Windows/Microsoft Live using OAuth PHP and MySQL

View Demo

How OAuth login works

Check out my posts on PHP OAuth to get better insights Login System with LinkedIn using OAuth php and mysql, Login System with Facebook with php and mysql, Login System with Google using OAuth php and mysql, Login System with Github using OAuth php and mysql.

How to create a Microsft/Windows Application for website

Create an aplication to get CLIENT ID and SECRET KEY. Given below is the powerpoint presentation which will guide you through the process of making the application.


Integration of Windows/Microsoft Live with OAuth PHP and MySQL

All the login process is very similar to the post I have posted before, please refer those if you still dont get it.

Set your configuration in config.php

define("CLIENT_ID", "your client id");
define("SECRET_KEY", "your secret key");
/* make sure the url end with a trailing slash, give your site URL */
define("SITE_URL", "http://demos.aarafacademy.com/login-system-with-windows-live/");
/* the page where you will be redirected for authorization */
define("REDIRECT_URL", SITE_URL."live_login.php");
/* Set the scope */
define("SCOPE", 'wl.basic wl.emails' );

define("LOGOUT_URL", SITE_URL."logout.php");


Set your login and logout URL in index.php and home.php
<a href="live_login.php">
  Login with Microsoft

<a href="<?php echo LOGOUT_URL; ?>">

In live_login.php. Create an oauth object and try to connect with live account and catch the response. If it returned any error send back user to index page.

if ($_GET["error"] <> "") {
  // in case if user cancel the login. redirect back to home page.
  $_SESSION["e_msg"] = $_GET["error"];
$client = new oauth_client_class;

$client->debug = false;
$client->debug_http = true;
$client->redirect_uri = REDIRECT_URL;

$client->client_id = CLIENT_ID;
$application_line = __LINE__;
$client->client_secret = SECRET_KEY;

if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
 die('Set CLIENT ID and SERCRET KEY');

$client->scope = SCOPE;
if (($success = $client->Initialize())) {
  if (($success = $client->Process())) {
    if (strlen($client->authorization_error)) {
      $client->error = $client->authorization_error;
      $success = false;
    } elseif (strlen($client->access_token)) {
      $success = $client->CallAPI(
              'https://apis.live.net/v5.0/me', 'GET', array(), array('FailOnAccessError' => true), $user);

  $success = $client->Finalize($success);
if ($client->exit) exit;

Add/update database on the basis of response.
if ($success) {
  $email = $user->emails->account;
  if ($email == "") {
    $email = $user->emails->preferred;

  // Now check if user exist with same email ID
  $sql = "SELECT COUNT(*) AS count from users where email = :email_id";
  try {
    $stmt = $DB->prepare($sql);
    $stmt->bindValue(":email_id", $email);
    $result = $stmt->fetchAll();

    if ($result[0]["count"] > 0) {
      // User Exist 

      $_SESSION["name"] = $user->name;
      $_SESSION["email"] = $email;
      $_SESSION["new_user"] = "no";
    } else {
      // New user, Insert in database
      $sql = "INSERT INTO `users` (`name`, `email`) VALUES " . "( :name, :email)";
      $stmt = $DB->prepare($sql);
      $stmt->bindValue(":name", $user->name);
      $stmt->bindValue(":email", $email);
      $result = $stmt->rowCount();
      if ($result > 0) {
        $_SESSION["name"] = $user->name;
        $_SESSION["email"] = $email;
        $_SESSION["new_user"] = "yes";
        $_SESSION["e_msg"] = "";
  } catch (Exception $ex) {
    $_SESSION["e_msg"] = $ex->getMessage();

  $_SESSION["user_id"] = $user->id;
} else {
  $_SESSION["e_msg"] = $client->error;

View Demo
Share to download the Source Codes for FREE!
We're glad to give free downloads, but we need your love to carry on making that.
Please support us by sharing the page.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.


Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Chally on

I’ve been following all of your Oauth tutorials. Very easy to integrate into my site. Are you planning to do a Twitter login system?

karpagavalli on

hi sir
how ti extra add in label input but not send email plz call me

Shahrukh Khan on

i dont understand. please explain

buy global traffic on

Great web site you have got here.. It’s difficult to find high quality writing like yours
nowadays. I really appreciate individuals like you!
Take care!!

shwe on

thanks for the tutorial…. it works perfectly on localhost but gives error after pushing on server, can you please help me.

Shahrukh Khan on

check with your App settings and path.

Raj Gohain on

hi, this tutorial works great with outlook.com id. But we have azure db(office365) license in our organization. When we try to login with our office365 id it shows id not found. I searches on google and change the paths (on oauth_client.php) as

$this->dialog_url = ‘https://login.microsoftonline.com/common/oauth2/authorize?client_id={CLIENT_ID}&scope={SCOPE}&response_type=code&redirect_uri={REDIRECT_URI}&state={STATE}’;
$this->access_token_url = ‘https://login.microsoftonline.com/common/oauth2/token’;

(line no 2058-59)

and define scope as
define(“SCOPE”, ‘Employees.Read.All’);

on config.php

Now it authenticates, but not able to retrive any user data. On the home.php, it shows Welcome back !!

Please help

Shahrukh Khan on

Hi. You have done good so far, see what else permission it is seeking from the documentation. also whats the error message are you getting?

Raj Gohain on

also on the home.php an error like —

it was not possible to access the OAuth access token: it was returned an unexpected response status 400 Response: {“error”:”invalid_resource”,”error_description”:”AADSTS50001: Resource identifier is not provided.\r\nTrace ID: 3104fc4b-2118-4451-a36a-8e410c580000\r\nCorrelation ID: aebff9c4-cb2b-4d51-87a3-c24fc6428924\r\nTimestamp: 2018-06-28 19:04:01Z”,”error_codes”:[50001],”timestamp”:”2018-06-28 19:04:01Z”,”trace_id”:”3104fc4b-2118-4451-a36b-8e410c580000″,”correlation_id”:”aebff9c4-cb2b-4d51-87a3-c24fc6428924″}


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.